【问题标题】:list view onitemselectedlistener error?列表视图 onitemselectedlistener 错误?
【发布时间】:2011-10-28 18:39:38
【问题描述】:

我创建了一个简单的列表视图。我有两个列表视图,我的第一个列表视图只显示一个项目名称是水果的项目。如果我在列表视图中单击水果,它将转到第二个列表视图。第二个列表视图显示许多水果名称。如果我选择一种水果,我想在我的第一个列表视图中显示该水果名称(我想在我的第一个列表视图中绑定该水果名称)所以,我正在使用 onitemselectedlistener 但它不起作用。如果我单击我的第二​​个列表视图水果名称没有任何反应....请检查我的源代码并帮助我....

第一个列表视图:

     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);                              }} 

第二个列表视图:

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){
 }});

}}

【问题讨论】:

  • 嗨,我在这一行中设置了调试 mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 我的应用程序崩溃了。我认为这行不适合我...请帮助我...跨度>

标签: android listview


【解决方案1】:

您的问题是您没有将第二个列表中选择的内容传递回第一个列表。您需要将选择的内容包含在您的意图中,然后在您的第一个活动中,您需要检查它是否包含在它开始的意图中。所以你会是这样的。

在你的第二个活动中:

mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){
     Intent in2 =new Intent(listview.this, bview.class);
     in2.putExtra("SELECTED_ITEM", (String)arg0.getItemAtPosition(arg2));
     startActivity(in2);
  }
  public void onNothingSelected(AdapterView<?>arg0){
}});

在您的第一个活动中,您需要在 onCreate 方法中添加以下代码:

Intent givenIntent = getIntent();
if(givenIntent.hasExtra("SELECTED_ITEM")){
  String selectedItem = givenIntent.getStringExtra("SELECTED_ITEM");
  //now do what ever you want with the face that you have the item that was selected
}
else{
  //in here you know the activity was started by selecting something from your second
  //activity. so do what you need to do in that case here.
}

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多