使用ItemClick侦听器将ListView元素复制到Android中相同Activity中的另一个listview

时间:2016-03-04 13:04:52

标签: android listview android-arrayadapter

我想一次又一次点击ListView项目,然后将我点击的项目转移到旁边的ArrayList。我正在使用自定义数组适配器,但我不能告诉PlayerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { Integer i = (int) (long) id; //Get Values from listview String PlayertxtName = ((TextVi ew)view.findViewById(R.id.txtPlayerName)).getText().toString(); String PlayertxtPos = ((TextView)view.findViewById(R.id.txtPlayerPosition)).getText().toString(); String PlayertxtNumber = ((TextView)view.findViewById(R.id.txtPlayerNumber)).getText().toString(); //define an ArrayList of Player type ArrayList<Player> GameRoster = new ArrayList<Player>(); Player td = new Player(); td.setPlayerId(i); td.setPlayerName((PlayertxtName)); td.setPlayerNumber(PlayertxtNumber); td.setPlayerPosition(PlayertxtPos); //Add player to Arraylist GameRoster.add(td); PlayerArrayAdapter arrayAdapter = new PlayerArrayAdapter(getBaseContext(),GameRoster); //Set the adapter GameList.setAdapter(arrayAdapter); //refresh data on listview arrayAdapter.notifyDataSetChanged(); } }); 更改索引并添加下一个元素。

fgetc()

1 个答案:

答案 0 :(得分:0)

显示以下代码以复制listview数据。

private String [] rock ={"The Beatles", "The Rolling Stones", "Jimi Hendrix", "Elvis Presley", "Pink Floyd", "AC/DC"};
private String [] pop ={"Michael Jackson", "Beyonce", "Rihanna", "Justin Bieber", "Lady Gaga", "Katy Perry"};

private List rocklist = new ArrayList();
private List poplist = new ArrayList();
private ArrayAdapter adapterRock;
private ArrayAdapter adapterPop;
private ListView mListView1;
private ListView mListView2;

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for(String a : rock){
        rocklist.add(a);
    }
    for (String b : pop){
        poplist.add(b);
    }

    mListView1 = (ListView)findViewById(R.id.listView1);
    mListView2 = (ListView)findViewById(R.id.listView2);
    adapterRock =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rocklist);
    mListView1.setAdapter(adapterRock);
    adapterPop = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, poplist);
    mListView2.setAdapter(adapterPop);

    mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            poplist.add(rocklist.get(position));
            adapterPop.notifyDataSetChanged();
        }
    });

}

它可能有用,如果你想要保存任何特定行的数据,那么你可以通过bean获取它。