我正在尝试向用户显示一条吐司消息,以显示他选择的项目。我已经从另一个类的意图上传递了该列表,并在其代码如下的类中接收了它:
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项中的值放入?
最佳答案
如果您有索引和数组列表,则可以按索引引用集合中的字符串:
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设置为一个字段,以便能够从其他方法引用它。
关于android - 单击可单击列表时显示 ListView 项选项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13680216/