简体   繁体   English

Android:如何获取ListView中单击的项目位置?

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

The code of my listview is like: 我的listview代码如下:

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. 但是,每次单击列表中的项时,getSelectedItemId()方法都会返回无效值。

PS. PS。 I tried getSelectedItemPosition(), it also returns an invalid value, which is -1. 我尝试了getSelectedItemPosition(),它还返回一个无效值,即-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. 您已经将ID直接传递给了您,它是onItemClick方法的第4个参数。

The docs have the following method signature for onItemClick : 文档具有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. position视图在适配器中的位置。

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. 我假设你想要这个位置,这样你就可以得到存储在与listview中该点相关联的游标中的一些数据。 What you should do is move your onClickListener assignment into the bindView method like this: 你应该做的是将onClickListener赋值移动到bindView方法中,如下所示:

 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
    }
  });   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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