Android - 将项目从一个列表视图复制到另一个列表视图

时间:2015-11-03 15:52:38

标签: java android listview mobile android-listview

我一直在研究这个Android项目并一直在努力。我目前有两个列表视图,都显示数据。

我不确定在点击该项目时如何将项目从一个列表视图复制到另一个列表视图。

这是我的代码:

public class MainActivity extends Activity {

private ListView mListView1, mListView2;

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

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

    mListView1 = (ListView)findViewById(R.id.listView1);
    mListView2 = (ListView)findViewById(R.id.listView2);

    mListView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rock));
    mListView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pop));

    ListUtils.setDynamicHeight(mListView1);
    ListUtils.setDynamicHeight(mListView2);
}


public static class ListUtils {
    public static void setDynamicHeight(ListView mListView) {
        ListAdapter mListAdapter = mListView.getAdapter();
        if (mListAdapter == null) {
            // when adapter is null
            return;
        }
        int height = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < mListAdapter.getCount(); i++) {
            View listItem = mListAdapter.getView(i, null, mListView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            height += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
        mListView.setLayoutParams(params);
        mListView.requestLayout();
    }
}

}

然后是我的main.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Rock" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="#B29090" >
    </ListView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center_vertical"
        android:text="Pop" />

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="#4A9C67" >
    </ListView>
</LinearLayout>

例如,当点击披头士乐队时,我希望将其复制到流行列表视图中。

我已经尝试过使用它(下面的例子在另一个答案中找到)但它不起作用,因为它说listview没有add()或notifyDataSetChange()方法。

listView1.setOnItemClickListener(this);

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    datasListView2.add(valuesListView1.get(position));
    listView2.notifyDataSetChange();
}

2 个答案:

答案 0 :(得分:0)

所以你有2个名单,摇滚和流行吧?并且当你点击第一个listView(显示摇滚列表的那个)上的项目时,你想要将该项目复制到第二个listView(显示弹出列表的那个)。

您只需将该项添加到“pop”列表,然后在listview适配器上调用notifyDataSetChange()。

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    pop.add(rock.get(position));
    mListView2.getAdapter().notifyDataSetChange();
}

这应该很简单但是你可能不想修改那个“pop”列表而只修改适配器中显示的列表?

在这种情况下,您应该在每个适配器中存储数组的副本,这样您就可以在不影响主列表的情况下修改它。

要实现这一点,您需要继承您正在使用的适配器。

希望这有帮助。

答案 1 :(得分:0)

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

}