我在一项活动中有两个ListView
个。我想通过ListView
Button
将所选项目的值从第一个onClick()
转移到第二个<?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.xml:
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);
}
});
}
}
活动:
{{1}}
答案 0 :(得分:1)
请尝试这样做。
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);
}
});
希望这有助于您解决问题。
答案 1 :(得分:1)
好。我得到了你的目标,所以我继续修改你的代码。这是我得到的:
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
的代码中,您检索了所选项目,将其存储在{{1}中并将其设置为String[]
作为其值,但仅在单击按钮时为第二个adapter2
设置适配器。更简单的方法就像在我提供的示例代码中,只是将选定的项目文本存储到String变量,然后只有单击按钮,执行传输(设置适配器/将值添加到适配器),这样,更少不必要的行动已经完成。
有关ListView
的更多信息,请查看here。
希望这能以某种方式帮助你。祝你好运,编码愉快。 :)