Note that there are some explanatory texts on larger screens.

plurals
  1. POlistview onclick android
    text
    copied!<p>I am creating an app that has a contacts list inside it. Currently the list works and displays all of my contacts just the way i want it. However i need to add a click function to them. I want to call them once i press one of them. How can i do that? I already have an onListItemClick method in the same activity due to an rss reader. How can i filter it out? Take a look at my code:</p> <pre><code> //Load contacts into ListView on people screen contactListView = (ListView) findViewById(R.id.contactsListView); contactstock = new ArrayList&lt;ContactStock&gt;(); mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.DISPLAY_NAME + " ASC"); int number = mCursor.getColumnIndex(Phone.NUMBER); int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME); while (mCursor.moveToNext()) { String phName = mCursor.getString(name); String phNumber = mCursor.getString(number); contactstock.add(new ContactStock(phName, phNumber)); } contactListView.setAdapter(new ContactListAdapter(MainActivity.this, contactstock)); </code></pre> <p>ContactStock.java:</p> <pre><code>public class ContactStock { private String name; private String number; public ContactStock(String name, String number) { this.name = name; this.number = number; } public void setName(String name) { this.name = name; } public void setNumber(String number) { this.number = number; } public String getName() { return this.name; } public String getNumber() { return this.number; } } </code></pre> <p>ContactListAdapter.java:</p> <pre><code>public class ContactListAdapter extends ArrayAdapter { private final Activity activity; private final List stocks; public ContactListAdapter(Activity activity, List objects) { super(activity, R.layout.listview_detail_tab_contact_list, objects); this.activity = activity; this.stocks = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; ContactStockView sv = null; if (rowView == null) { // Get a new instance of the row layout view LayoutInflater inflater = activity.getLayoutInflater(); rowView = inflater.inflate( R.layout.listview_detail_tab_contact_list, null); // Hold the view objects in an object, // so they don't need to be re-fetched sv = new ContactStockView(); sv.name = (TextView) rowView.findViewById(R.id.contact_name); sv.number = (TextView) rowView.findViewById(R.id.contact_number); // Cache the view objects in the tag, // so they can be re-accessed later rowView.setTag(sv); } else { sv = (ContactStockView) rowView.getTag(); } // Transfer the stock data from the data object // to the view objects ContactStock currentStock = (ContactStock) stocks.get(position); sv.name.setText(currentStock.getName()); sv.number.setText(currentStock.getNumber()); // TODO Auto-generated method stub return rowView; } protected static class ContactStockView { protected TextView name; protected TextView number; } } </code></pre> <p>My current listview code(for rss viewer):</p> <pre><code>@Override protected void onListItemClick(ListView l, View v, int position, long id) { Uri uri = Uri.parse(links.get(position).toString()); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } </code></pre> <p>ListView row xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:src="@drawable/defaultavatar" /&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/contact_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:text="Who am I" android:textAppearance="?android:attr/textAppearanceLarge" /&gt; &lt;TextView android:id="@+id/contact_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="000000000" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>I got the phone number from the contact i tapped by using this code:</p> <pre><code>contactstock.get(arg2).getNumber() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload