简体   繁体   中英

android listview listener does not work?

here is my layuout main.xml ;

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" android:visibility="visible" android:clickable="true"/>

</LinearLayout>

Here is the MainActivity which extends ListActivity;

 public class MainActivity extends ListActivity {
        private final List<SpinnerEntry> spinnerContent = new LinkedList<SpinnerEntry>();
        private ListView contactListView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            contactListView=getListView();
queryAllRawContacts();
registerForContextMenu(getListView());

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ContactDetailsActivity.class);
        i.putExtra("hello", id);
        // Activity returns an result if called with startActivityForResult

        startActivity(i);
    }

private void queryAllRawContacts() {


        final String[] projection = new String[] {
                RawContacts.CONTACT_ID,                 // the contact id column
                RawContacts.DELETED                     // column if this contact is deleted
        };

        final Cursor rawContacts = managedQuery(
                RawContacts.CONTENT_URI,                // the uri for raw contact provider
                projection, 
                null,                                   // selection = null, retrieve all entries
                null,                                   // not required because selection does not contain parameters
                null);                                  // do not order

        final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
        final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);

        spinnerContent.clear();
        if(rawContacts.moveToFirst()) {                 // move the cursor to the first entry
            while(!rawContacts.isAfterLast()) {         // still a valid entry left?
                final int contactId = rawContacts.getInt(contactIdColumnIndex);
                final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);
                if(!deleted) {
                    spinnerContent.add(queryDetailsForContactSpinnerEntry(contactId));
                }
                rawContacts.moveToNext();               // move to the next entry
            }
        }

        rawContacts.close();
        final ContactsSpinnerAdapater adapter = new ContactsSpinnerAdapater(spinnerContent, this);
        contactListView.setAdapter(adapter);
    }

And here is my customadapter class called ContactsSpinnerAdapater ;

public class ContactsSpinnerAdapater extends BaseAdapter implements
        SpinnerAdapter{
    private final List<SpinnerEntry> content;
    private final Activity activity;

    public ContactsSpinnerAdapater(List<SpinnerEntry> content,
            Activity activity) {
        super();
        this.content = content;
        this.activity = activity;
    }

    public int getCount() {
        return content.size();
    }

    public SpinnerEntry getItem(int position) {
        return content.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView,
            ViewGroup parent) {
        final LayoutInflater inflater = activity.getLayoutInflater();   // default layout inflater
        final View spinnerEntry = inflater.inflate(
                R.layout.row, null);                // initialize the layout from xml
        final TextView contactName = (TextView) spinnerEntry
                .findViewById(R.id.cName);
        final ImageView contactImage = (ImageView) spinnerEntry
                .findViewById(R.id.userIcon);
        final SpinnerEntry currentEntry = content.get(position);    
        contactName.setText(currentEntry.getContactName());
        contactImage.setImageBitmap(currentEntry.getContactPhoto());
        spinnerEntry.setOnClickListener(new OnClickListener(){


            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }});
        return spinnerEntry;
    }




}

The problem is that whatever I tried I could not start the ContacDetailsActivity intent when any of the list item is clicked. Basically, the listener some how is not activated. So could you please have a look at the code and tell me where I am missing it?

You've put your intent starting code in the wrong place. You need to put this code:

    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, ContactDetailsActivity.class);
    i.putExtra("hello", id);
    // Activity returns an result if called with startActivityForResult

    startActivity(i);

in your getView method in your adapter like this

public View getView(int position, View convertView,
        ViewGroup parent) {
    final int myFinalPosition = position;
    final LayoutInflater inflater = activity.getLayoutInflater();   // default layout inflater
    final View spinnerEntry = inflater.inflate(
            R.layout.row, null);                // initialize the layout from xml
    final TextView contactName = (TextView) spinnerEntry
            .findViewById(R.id.cName);
    final ImageView contactImage = (ImageView) spinnerEntry
            .findViewById(R.id.userIcon);
    final SpinnerEntry currentEntry = content.get(position);    
    contactName.setText(currentEntry.getContactName());
    contactImage.setImageBitmap(currentEntry.getContactPhoto());
    spinnerEntry.setOnClickListener(new OnClickListener(){


        public void onClick(View arg0) {
          Intent i = new Intent(activity, ContactDetailsActivity.class);
          i.putExtra("hello", getItemId(myFinalPosition));
          // Activity returns an result if called with startActivityForResult

          activity.startActivity(i);

        }});
    return spinnerEntry;
}

Note the use of the activity variable to start up the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM