我制作了一个带有按钮和文本视图的自定义列表视图。我想做的是-单击按钮,我想获取列表视图的索引。
我使用 HashMap 来存储键值对。
这是我的活动代码:
package com.hisham.listviewexhisham;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv= (ListView)findViewById(R.id.listview);
final TextView tvTesting = (TextView)findViewById(R.id.tvTesting);
// create the grid item mapping
String[] from = new String[] {"quantity", "item", "cost", "remove"};
int[] to = new int[] { R.id.quantity, R.id.item, R.id.cost, R.id.remove};
ArrayList<String> productNames = new ArrayList<String>();
productNames.add("Trumbull 125");
productNames.add("Trumbull 135");
productNames.add("Trumbull 17'' Monitor");
productNames.add("Trumbull Keyboard");
productNames.add("Trumbull 2TB HD");
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < productNames.size(); i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("quantity", "" + i);
map.put("item", productNames.get(i));
map.put("cost", "$ 600.99");
map.put("remove", "X");
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid, from, to);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
tvTesting.setText("Id is - " +arg2);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:width="20dip" />
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.01"
android:text="Trumbull"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/cost"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="$ 500.99" />
<Button
android:id="@+id/remove"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="X" />
</LinearLayout>
</LinearLayout>
这是我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTesting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>