Listview OnItemClickListener()用于自定义布局

时间:2013-12-27 08:46:18

标签: android listview android-listview

我的自定义布局列表视图

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:stretchColumns="1" >

<TableRow>

    <TextView
     android:id="@+id/item1"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:padding="10dip"
     android:textColor="@color/white_color"
     android:layout_margin="4dip"
     android:layout_weight="0.15"
      />

     <TextView
     android:id="@+id/item2"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:padding="10dip"
     android:textColor="@color/white_color"
     android:layout_margin="4dip"
     android:layout_weight="0.53"
      />

      <TextView
     android:id="@+id/item3"
     android:layout_width="0dip"
     android:layout_height="match_parent"
     android:layout_gravity="right"
     android:gravity="right"
     android:padding="10dip"
     android:textColor="@color/white_color"
     android:layout_margin="4dip"
     android:layout_weight="0.32"
      />

</TableRow>

使用arrayadapter的Listview:

ListView lv = (ListView) findViewById(R.id.prodlist);
String[] from = new String[] { "rowid", "col_1", "col_2" };
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3 };

// prepare the list of all records
ArrayList<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 20; i++) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("rowid", "" + i);
    map.put("col_1", name);
    map.put("col_2", qnt);
}

fillMaps.add(map);

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.custom_layout, from, to);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //How to get item2 text value?..

    }
});

所有三个textview都添加在arrayadapter中。我想要做的就是当我点击listview时,如何获取textview文本(r.id.item2)。

5 个答案:

答案 0 :(得分:1)

onItemClick内:

HashMap<String, String> hashReference = (HashMap)parent.getItemAtPosition(position);
String name = hashReference.get("col_1"); //the keys that you saved above.
String qnt = hashReference.get("col_2");

答案 1 :(得分:1)

您可以使用View view onItemClick()的{​​{1}}参数,

ListView

答案 2 :(得分:1)

String item1= ((TextView) view.findViewById(R.id.item1)).getText().toString();
String item2= ((TextView) view.findViewById(R.id.item2)).getText().toString();
String item3= ((TextView) view.findViewById(R.id.item3)).getText().toString();

答案 3 :(得分:1)

您可以使用点击项目的HashMap从列表中获取position

如下所示

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                //How to get item2 text value?..
                HashMap<String, String> map = fillMaps.get(position);
                String col1Value = map.get("col_1");
                String col2Value = map.get("col_2");
            }
          });

答案 4 :(得分:1)

// try this way
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           String item1 = fillMaps.get(position).get("rowid");
           String item2 = fillMaps.get(position).get("col_1");
           String item3 = fillMaps.get(position).get("col_2");
    }
});