我正在尝试显示一个Toast消息,供用户显示他选择的项目。我已将该列表作为另一个类的意图传递,并在类中接收它,其代码如下:
public class ListViewDelete extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_list_view_delete);
final Intent intent = getIntent();
final Bundle extras = getIntent().getExtras(); //gets the GWID
final MySQLitehelper dbhelper = new MySQLitehelper(this);
ArrayList<String> thelist = new ArrayList<String>(extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE));
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE)));
}
public void onListItemClick(ListView parent, View view, int position, long id)
{
Toast.makeText(this, "You have selected", Toast.LENGTH_LONG).show();
}
}
在最后一个onListItemClick中,如何在“你选择了”之后自定义,我可以把上面定义的arraylist项中的值设置好?
答案 0 :(得分:2)
public void onListItemClick(ListView parent, View view, int position, long id)
{
Toast.makeText(this, "You have selected"+position, Toast.LENGTH_LONG).show();
}
答案 1 :(得分:1)
如果您有索引和arraylist,那么您可以通过索引引用集合中的字符串:
public class ListViewDelete extends ListActivity {
private ArrayList<String> thelist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_list_view_delete);
final Intent intent = getIntent();
final Bundle extras = getIntent().getExtras(); //gets the GWID
final MySQLitehelper dbhelper = new MySQLitehelper(this);
thelist = new ArrayList<String>(extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE));
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE)));
}
public void onListItemClick(ListView parent, View view, int position, long id)
{
Toast.makeText(this, "You have selected" + thelist.get(position), Toast.LENGTH_LONG).show();
}
}
请注意,我将arrayList作为一个字段,以便能够从另一个方法引用它。