【问题标题】:how to bind an item selected in list view to the other list?如何将列表视图中选择的项目绑定到另一个列表?
【发布时间】:2023-04-01 00:00:01
【问题描述】:

我创建了两个简单的列表视图。在第一个列表视图中,我将类别作为项目名称。这是我列入清单一的唯一项目。在列表视图中,我放了很多项目,例如橙色、苹果、芒果、香蕉等。我想知道的是,如果用户在第二个列表视图中选择了一个特定项目,我希望所选项目是显示在第一个列表视图中。因此很容易识别用户之前选择的项目。请帮帮我....

这是我的第一个列表视图代码:

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

}}

我使用了 onitemselected,但如果单击橙色,则不会发生任何事情。我应该在我的代码中出错的地方......

【问题讨论】:

    标签: android listview


    【解决方案1】:

    在第二个列表视图中,使用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
    }
    

    【讨论】:

    • 我应该去哪里实现这个源代码到我的源代码...我是 android 应用程序的新手...请告诉我...我发布我的源代码...请检查并帮帮我....
    • 就像我在回答中所说的,这段代码应该放在第二个活动的 onCreate 方法中。
    • 现在我更改了我的代码,但是什么都不会发生......请检查并告诉我......
    • 嗯,我没有提供完整的示例。您必须在我放置 cmets 的位置填写您自己的详细信息。
    【解决方案2】:

    在第二个ListView的OnItemClick中,可以设置第一个ListView的选中项,见Android - setSelected in OnItemClick in ListView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多