如何在setOnItemClickListener上将列表视图记录显示到第二个列表视图中

时间:2014-09-21 16:55:35

标签: android listview

我创建了列表视图,我想要做的是,当用户点击第一个列表视图时,所选记录应显示在第二个列表中,在我的代码中显示在文本视图中,但我想在第二个列表中显示该记录列表视图所以请给我代码/想法如何做到这一点,因为我是android的新手..

public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv2,tv3,tv4,tv5;
List<TextView> arrayTV = new ArrayList<TextView>();
ListView li,li2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

li=(ListView)findViewById(R.id.listView1);
li2=(ListView)findViewById(R.id.listView2);

tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);

arrayTV.add(tv);
arrayTV.add(tv2);
arrayTV.add(tv3);
arrayTV.add(tv4);
arrayTV.add(tv5);

ArrayAdapter<String> adapter=new ArrayAdapter<String>   (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);   
li.setAdapter(adapter); 
li.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
    // TODO Auto-generated method stub

    int itm=position;       
 Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
    arrayTV.get(position).setText(""+li.getItemAtPosition(position));
}
  });   
 }

这是我的XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rt"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight=".5" >

</ListView>

1 个答案:

答案 0 :(得分:1)

在第一个listview的onitemclick中为第二个listview设置适配器

List<String> a = new ArrayList<String>();

     li.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {

        String clickedItem =item[postion];
if(!a.contains(clickedItem))
        a.add(clickedItem);
        String[] newitem = new String[a.size()];
        a.toArray(newitem);
            ArrayAdapter<String> adapter=new ArrayAdapter<String>   (MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1, newitem);   
                li2.setAdapter(adapter);
            }
              });