简体   繁体   中英

Android: how to get the clicked item position in the ListView?

The code of my listview is like:

CourseDataAdapter mCourseListAdapter = new CourseDataAdapter(this, R.layout.coursesearchviewrow, mCursor);

list.setAdapter(mCourseListAdapter);

list.setItemsCanFocus(false);

list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
        long index = arg0.getSelectedItemId();
        listDialog.dismiss();

    }

});

and I use my customized cursor adapter, my code is here:

public class CourseDataAdapter extends ResourceCursorAdapter {
    private TextView courseType;
    private TextView courseDays;
    private TextView courseTime;

    public CourseDataAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater mInflater = (LayoutInflater)context.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);
        return mInflater.inflate(R.layout.coursesearchviewrow, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        courseType = (TextView) view.findViewById(R.id.course_search_type_view);
        courseDays = (TextView) view.findViewById(R.id.course_search_days_view);
        courseTime = (TextView) view.findViewById(R.id.course_search_time_view);

        courseType.setText(cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_TYPE)));
        courseDays.setText(CourseDataHandler.daysStringProcessor(cursor.getInt(cursor.getColumnIndex(CourseDbAdapter.KEY_DAYS))));
        courseTime.setText(CourseDataHandler.courseTimeProcessor(cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_START_TIME)), cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_END_TIME))));
    }

}

However, everytime when I clicked the item in the list, it the getSelectedItemId() method returns a invalid value.

PS. I tried getSelectedItemPosition(), it also returns an invalid value, which is -1.

So how can I get the Position of which item I clicked?

A list item being selected is a different concept from having pressed it, and doesn't apply in this case. You already have the ID passed to you directly, it's the 4th argument of the onItemClick method.

The docs have the following method signature for onItemClick :

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

and this seems to be what you're looking for:

position The position of the view in the adapter.

I'm assuming you want the position so you can get at some data that stored in the cursor associated with that point in the listview. What you should do is move your onClickListener assignment into the bindView method like this:

 public void bindView(View view, Context context, Cursor cursor) {
    courseType = (TextView) view.findViewById(R.id.course_search_type_view);
    courseDays = (TextView) view.findViewById(R.id.course_search_days_view);
    courseTime = (TextView) view.findViewById(R.id.course_search_time_view);

    courseType.setText(cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_TYPE)));
    courseDays.setText(CourseDataHandler.daysStringProcessor(cursor.getInt(cursor.getColumnIndex(CourseDbAdapter.KEY_DAYS))));
    courseTime.setText(CourseDataHandler.courseTimeProcessor(cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_START_TIME)), cursor.getString(cursor.getColumnIndex(CourseDbAdapter.KEY_END_TIME))));



  view.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
       //in here you now have access to the cursor
    }
  });   
}

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