如何将第一个 ListView 的选定值传输到第二个 ListView?
How to transfer selected value of first ListView to second ListView?
我有两个 ListView
合二为一 activity。我想通过一个Button
s onClick()
将选中项的值从第一个ListView
转移到第二个ListView
。请在下面查看我的代码。
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.edkul.vimal.edkul.Library">
<TextView
android:id="@+id/textView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Library"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView27"
android:layout_marginTop="35dp"
android:orientation="horizontal"
android:weightSum="2">
<ListView
android:id="@+id/list_item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#88FF0000"
android:padding="3dp">
</ListView>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="3dp"
android:weightSum="2">
<Button
android:id="@+id/nextButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="@drawable/circular_button"
android:drawableTop="@android:drawable/ic_media_next" />
<Button
android:id="@+id/previousButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="@drawable/circular_button"
android:drawableTop="@android:drawable/ic_media_previous" />
</LinearLayout>
<ListView
android:id="@+id/list_item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Activity:
package com.edkul.vimal.edkul;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Library extends Activity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
Button btn1,btn2;
ArrayAdapter adapter1,adapter2;
String[] data;
ListView listView1,listView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
listView1 = (ListView) findViewById(R.id.list_item1);
listView2 = (ListView) findViewById(R.id.list_item3);
adapter1 = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
listView1.setAdapter(adapter1);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) (listView1.getItemAtPosition(myItemInt));
Log.v("Print Here", selectedFromList.toString());
data[0] = selectedFromList.toString();
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_listview, data[0]);
}
});
btn1 = (Button) findViewById(R.id.nextButton);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listView2.setAdapter(adapter2);
}
});
}
}
这样试试
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String clickedItem = mobileArray[postion];
if(!arrayData.contains(clickedItem))
arrayData.add(clickedItem);
String[] newitem = new String[arrayData.size()];
arrayData.toArray(newitem);
adapter2 = new ArrayAdapter<String>(Library.this, android.R.layout.simple_list_item_1,android.R.id.text1, newitem);
}
});
这里的 arrayData 是你的列表数组,全局定义它。
List<String> arrayData = new ArrayList<String>();
然后点击按钮设置您的适配器。
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listView2.setAdapter(adapter2);
}
});
希望本文能帮助您解决问题。
好的。我有点明白你的目标是什么行为,所以我继续修改你的代码。这是我得到的结果:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
List<String> mobileArray2 = new ArrayList<String>();
String selectedText;
Button btn1, btn2;
ArrayAdapter adapter1, adapter2;
String[] data;
ListView listView1, listView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
// Initialize stuff..
listView1 = (ListView) findViewById(R.id.list_item1);
listView2 = (ListView) findViewById(R.id.list_item3);
btn1 = (Button) findViewById(R.id.nextButton);
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mobileArray);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mobileArray2);
listView1.setAdapter(adapter1);
listView2.setAdapter(adapter2);
// Set set item click listener for first listview..
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
selectedText = (String) (listView1.getItemAtPosition(myItemInt));
Log.v("Print Here", selectedText);
}
});
// set click listener for button 1..
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendToOtherListView(selectedText, listView2);
}
});
}
// function that accepts the string to transfer and the listview to where it will be transferred.
private void sendToOtherListView(String strToTransfer, ListView lvDestination) {
// gets the adapter of the destination listview..
ArrayAdapter<String> adapter = (ArrayAdapter<String>) lvDestination.getAdapter();
// adds the string to transfer to the destination listview adapter.
adapter.add(strToTransfer);
}
}
这只是基本代码。没有完全优化,只是一些你可以用作指南的东西。
两个提示(这些提示正是 我 所想和 我 所见所遗漏或可以被添加,可能与最佳实践不同,你应该进一步搜索它们):
我认为使用 List<String>
比 String[]
更容易,因为铸造原因(这是我指的那个我不确定是否是最佳实践,只是我的看法).
添加空检查器以避免 NPE。
几乎一切都很好,我猜你只需要在数据操作上多练习.. 例如,在你的 setOnItemClickListener
代码中,你检索了所选项目,将其存储在 String[]
并将其设置为 adapter2
作为其值,但仅在单击按钮时为第二个 ListView
设置适配器。更简单的方法就像我提供的示例代码一样,只需将选定的项目文本存储到一个字符串变量中,然后只有在单击按钮后,才进行传输(将 adapter/adding 的值设置为适配器),这样,减少了不必要的操作。
有关 ListView
的更多信息,请查看 here。
希望这对您有所帮助。祝你好运,编码愉快。 :)
我有两个 ListView
合二为一 activity。我想通过一个Button
s onClick()
将选中项的值从第一个ListView
转移到第二个ListView
。请在下面查看我的代码。
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.edkul.vimal.edkul.Library">
<TextView
android:id="@+id/textView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Library"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView27"
android:layout_marginTop="35dp"
android:orientation="horizontal"
android:weightSum="2">
<ListView
android:id="@+id/list_item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#88FF0000"
android:padding="3dp">
</ListView>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="3dp"
android:weightSum="2">
<Button
android:id="@+id/nextButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="@drawable/circular_button"
android:drawableTop="@android:drawable/ic_media_next" />
<Button
android:id="@+id/previousButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="@drawable/circular_button"
android:drawableTop="@android:drawable/ic_media_previous" />
</LinearLayout>
<ListView
android:id="@+id/list_item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Activity:
package com.edkul.vimal.edkul;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Library extends Activity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
Button btn1,btn2;
ArrayAdapter adapter1,adapter2;
String[] data;
ListView listView1,listView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
listView1 = (ListView) findViewById(R.id.list_item1);
listView2 = (ListView) findViewById(R.id.list_item3);
adapter1 = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
listView1.setAdapter(adapter1);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) (listView1.getItemAtPosition(myItemInt));
Log.v("Print Here", selectedFromList.toString());
data[0] = selectedFromList.toString();
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_listview, data[0]);
}
});
btn1 = (Button) findViewById(R.id.nextButton);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listView2.setAdapter(adapter2);
}
});
}
}
这样试试
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String clickedItem = mobileArray[postion];
if(!arrayData.contains(clickedItem))
arrayData.add(clickedItem);
String[] newitem = new String[arrayData.size()];
arrayData.toArray(newitem);
adapter2 = new ArrayAdapter<String>(Library.this, android.R.layout.simple_list_item_1,android.R.id.text1, newitem);
}
});
这里的 arrayData 是你的列表数组,全局定义它。
List<String> arrayData = new ArrayList<String>();
然后点击按钮设置您的适配器。
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listView2.setAdapter(adapter2);
}
});
希望本文能帮助您解决问题。
好的。我有点明白你的目标是什么行为,所以我继续修改你的代码。这是我得到的结果:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
List<String> mobileArray2 = new ArrayList<String>();
String selectedText;
Button btn1, btn2;
ArrayAdapter adapter1, adapter2;
String[] data;
ListView listView1, listView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
// Initialize stuff..
listView1 = (ListView) findViewById(R.id.list_item1);
listView2 = (ListView) findViewById(R.id.list_item3);
btn1 = (Button) findViewById(R.id.nextButton);
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mobileArray);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mobileArray2);
listView1.setAdapter(adapter1);
listView2.setAdapter(adapter2);
// Set set item click listener for first listview..
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
selectedText = (String) (listView1.getItemAtPosition(myItemInt));
Log.v("Print Here", selectedText);
}
});
// set click listener for button 1..
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendToOtherListView(selectedText, listView2);
}
});
}
// function that accepts the string to transfer and the listview to where it will be transferred.
private void sendToOtherListView(String strToTransfer, ListView lvDestination) {
// gets the adapter of the destination listview..
ArrayAdapter<String> adapter = (ArrayAdapter<String>) lvDestination.getAdapter();
// adds the string to transfer to the destination listview adapter.
adapter.add(strToTransfer);
}
}
这只是基本代码。没有完全优化,只是一些你可以用作指南的东西。
两个提示(这些提示正是 我 所想和 我 所见所遗漏或可以被添加,可能与最佳实践不同,你应该进一步搜索它们):
我认为使用
List<String>
比String[]
更容易,因为铸造原因(这是我指的那个我不确定是否是最佳实践,只是我的看法).添加空检查器以避免 NPE。
几乎一切都很好,我猜你只需要在数据操作上多练习.. 例如,在你的 setOnItemClickListener
代码中,你检索了所选项目,将其存储在 String[]
并将其设置为 adapter2
作为其值,但仅在单击按钮时为第二个 ListView
设置适配器。更简单的方法就像我提供的示例代码一样,只需将选定的项目文本存储到一个字符串变量中,然后只有在单击按钮后,才进行传输(将 adapter/adding 的值设置为适配器),这样,减少了不必要的操作。
有关 ListView
的更多信息,请查看 here。
希望这对您有所帮助。祝你好运,编码愉快。 :)