我创建了两个简单的列表视图。在第一个列表视图中,我将category作为项目名称。这是我列入清单一的唯一项目。在列表视图中我放了很多项目,例如橙色,苹果,芒果,香蕉等。我想知道的是,如果用户在第二个列表视图中选择一个特定项目,我希望所选项目是在第一个列表视图中显示。因此,很容易识别用户先前选择的项目。请帮帮我....
这是我的第一个列表视图代码:
public class bview extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, cat));
getListView().setTextFilterEnabled(true);
}
static final String[] cat = new String[] {
"Category", };
protected void onListItemClick(ListView parent, View v, int position,
long id) {
Intent intent= new Intent(bview.this,listview.class);
startActivity(intent);
//intent.putExtra("value", value);
}}
第二个listview代码:
public class listview extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.submain);
ListView mlistView = (ListView) findViewById(R.id.listview);
mlistView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new String[] {"orange", "apple","mango","banana","grapes"}));
mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){
Intent in2 =new Intent(listview.this, bview.class);
startActivity(in2);
}
public void onNothingSelected(AdapterView<?>arg0){
}});
}}
我使用了onitemselected但是如果点击橙色则不会发生任何事情。在我的代码中我应该出错...
答案 0 :(得分:0)
在第二个列表视图中,使用setOnItemSelectedListener。当用户单击其中一个项目时,将其添加到支持第一个ListView的数据结构中。所以你在第二次活动中在onCreate中做了类似的事情:
protected void onCreate(Bundle icicle){
//setup your listview here
myListview.setOnItemSelectedListener(new AdpaterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
//add particular item that was clicked to the backing datastructure
// of your listview in the first activity here
}
public void onNothingSelected(AdapterView<?> parent){
//don't do anything here
}
});
//any other stuff in your onCreate
}
答案 1 :(得分:0)
在第二个ListView的OnItemClick中,您可以设置第一个ListView的选定项目,请参阅Android - setSelected in OnItemClick in ListView