用于自定义ListView的ONItemClickListener

时间:2013-11-09 12:22:43

标签: android listview

我对android编程很新,因此无法自己解决很多问题。在过去的几个小时里,我试图根据用户点击列表视图编写代码来进行一些活动。基本上,我需要在下面做:

1)我已经成功生成了包含BOOK ID,BOOK NAME和WRITER信息的列表视图 2)如果我点击每一行,我想读取该行的BOOK ID,返回主页,然后在主页面上运行预定义的函数x(BOOK_ID)。

我试图搜索很多,但作为一个新手,一切都让我更加困惑。任何帮助将不胜感激。

Listview的单个块:     

    <TextView
        android:id="@+id/bookname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book Title" />
    <TextView
        android:id="@+id/bookid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book ID" />

    <TextView
        android:id="@+id/writer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Writer" />
</RelativeLayout>

Java类:

mylibmandbhandler db = new mylibmandbhandler(this);
        List<mylibman> allfld = db.getAllRecord();
        ArrayList<HashMap<String, String>> allbookList = new ArrayList<HashMap<String, String>>();
        for (mylibman cn : allfld) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(LIST_KEY_ID, Integer.toString(cn.getBookid()));
            map.put(LIST_KEY_NAME, cn.getBookname());
            map.put(LIST_KEY_WRITER, cn.getWriter());
            allbookList.add(map);
        }
        list=(ListView)findViewById(R.id.list);
        adapter=new MylibmanList(this, allbookList); //calling adapter class
        list.setAdapter(adapter);
// UPTO THIS POINT CODE IS WORKING FINE

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// NEED TO GET THE BOOKID OF THE ROW UPON CLICKING, GO BACK TO MAIN ACTIVITY, AND CALL A FUNCTION X()
            }
        });

3 个答案:

答案 0 :(得分:1)

在你的Adapter类中,实现像这样的getItem(int position)......

@Override
public Object getItem(int position) {
  return allBookList.get(position);
}

并像这样实现OnItemClickListener ..

 list.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, String> hashMap = (HashMap<String, String>) parent.getItemAtPosition(position);
        String string = hashMap.get(LIST_KEY_ID);
        int id = Integer.valueOf(string);
        Intent intent = new Intent();
        intent.putExtra("bookId",id);
        setResult(RESULT_OK,intent);
        finish();
     }
 });

以及如何启动结果活动,请参阅2Dee给出的link

答案 1 :(得分:0)

要检索ListView中对象的引用,可以在OnItemClick中使用适配器中的get方法:

String bookId = adapter.get(position).get(LIST_KEY_ID);
// then return to your MainActivity, using the startActivityForResult mechanism
Intent returnIntent = new Intent();
returnIntent.putExtra("bookId",bookId);
setResult(RESULT_OK,returnIntent);     
finish();

有关使用startActivityForResult here的详细信息。

答案 2 :(得分:-1)

使用setTag(bookID)将标记设置为getViewMylibmanList中要返回的视图,然后在onItemClick上检索该标记,例如view.getTag(); < / p>