简体   繁体   English

用于自定义布局的Listview OnItemClickListener()

[英]Listview OnItemClickListener() for custom layout

I have my listview with custom layout 我的列表视图具有自定义布局

custom_layout.xml 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>

Listview with arrayadapter: 具有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?..

    }
});

All three textview are added in the arrayadapter.All I want to do is when I click the listview,how to get the text of textview(r.id.item2). 这三个textview都添加到arrayadapter中。我要做的就是单击列表视图时如何获取textview(r.id.item2)的文本。

Inside your onItemClick : 在您的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");

You can use View view argument of your onItemClick() of ListView , 您可以使用ListView onItemClick()View view参数,

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

               TextView mTextView = (TextView)view.findViewById(r.id.item2); 
               String value = mTextView.getText().toString();
            }
 });
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();

You can get HashMap from the list by using position of clicked item. 您可以通过使用单击项的position从列表中获取HashMap

Like below 像下面

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

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

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