我正在使用带有List的onItemClickListener()。我想传递点击项目的名称,并将任意数字传递给列表的下一个实例。如何才能做到这一点? 编辑:这是班级:
public class ListViewA extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"col_1"};
int[] to = new int[] {R.id.item2};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1", "col_1_item_" + i);
fillMaps.add(map);
lv.setOnItemClickListener(onListClick);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
Intent i=new Intent(ListViewA.this,ListViewA.class);
startActivity(i);
}
};
}
答案 0 :(得分:5)
以下示例显示如何在特定位置传递onItemClick数据。
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
HERE YOU GOT POSITION FOR PERTICULAR ITEM
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("position", fillMaps.get(position));
startActivity(i);
}
});
是的,您需要将fillMaps
声明为全局。
public class ListViewA extends Activity{
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);......
答案 1 :(得分:2)
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("Key", fillMaps.get(position));
startActivity(i);
您可以在onItemClickListener中使用上述代码。
答案 2 :(得分:0)
在这种方法中,第三个方法是位置。所以你必须使用这个例子。您传递给listView的Arraylist包含listView上显示的数据。所以使用这种代码
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String data= mArrlist.get(position);
}
如果您正在使用String类型的arralist,否则如果您正在使用getter setter类的arraylist,那么使用
mArralist.get(potion).getName();
答案 3 :(得分:0)
你应该得到项目@paticular position
HashMap<String, String> item = adapterview.getAdapter().getItem(position);
根据要求检查您的情况,并从item
获取数据并进一步处理。
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("item", item);
startActivity(i);
答案 4 :(得分:0)
对于添加任意数字,您可以使用java的Random类,如下所示 随机mRandom = new Random(); int random = mRandom.nextInt(mMoveToList.size());
每次随机值返回int类型的随机变量。
答案 5 :(得分:0)
您可以使用getItem(int position)
SimpleAdapter
adapter
设为全局adapter.getItem(position)