Android中的ListView选择?
我的布局中有两个ListView和一个常用的行布局,该行重复了多次。我不知道如何在两个ListView上确定选择了哪个结果,以及如何获取在两个ListView中选择的TextView的数据 这是我的ListView XMLAndroid中的ListView选择?,android,android-listview,Android,Android Listview,我的布局中有两个ListView和一个常用的行布局,该行重复了多次。我不知道如何在两个ListView上确定选择了哪个结果,以及如何获取在两个ListView中选择的TextView的数据 这是我的ListView XML <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above=
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/footerLayout"
android:layout_below="@+id/sortFlightLayouts"
android:orientation="horizontal" >
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/lvArrivals"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
这是我放在这两个列表视图中的行布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/flightLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5sp"
android:layout_marginRight="10sp"
android:layout_marginTop="5sp"
android:layout_marginLeft="5dp"
android:src="@drawable/spicejet" />
<TextView
android:id="@+id/flightCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/flightLogo"
android:text="SpiceJet" />
<TextView
android:id="@+id/flightNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/flightCompanyName"
android:text="9W-123" />
<TextView
android:id="@+id/flightTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="12dp"
android:layout_toRightOf="@+id/flightLogo"
android:gravity="center_vertical"
android:text="6:00 - 7:00" />
<TextView
android:id="@+id/flightStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/flightTime"
android:layout_toRightOf="@+id/flightLogo"
android:layout_marginLeft="12dp"
android:text="1h 35m | Non Stop" />
<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/flightStop"
android:layout_below="@+id/flightStop"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/flightCompanyName"
android:text="Rs 20,000" />
</RelativeLayout>
在我的Activity.java中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
ArrayList<HashMap<String, String>> flightData = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 12; i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(FlightCompanyName, "SpiceJet");
map.put(FlightNumber, "SG-123");
map.put(FlightTime, "6:00 - 7:00");
map.put(FlightStop, "1h 30m | Non Stop");
map.put(FlightCost, "Rs 12,000");
// adding HashList to ArrayList
flightData.add(map);
}
onewayListView=(ListView)findViewById(R.id.lvDepartures);
ReturnListView=(ListView)findViewById(R.id.lvArrivals);
// Getting adapter by passing xml data ArrayList
onewaydata=new OneWayFlightResult(this, flightData);
onewayListView.setAdapter(onewaydata);
returndata=new ReturnFlightResult(this, flightData);
ReturnListView.setAdapter(returndata);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.test);
ArrayList flightData=新的ArrayList();
对于(int i=0;i<12;i++){
//创建新的HashMap
HashMap=newHashMap();
//将每个子节点添加到HashMap key=>value
map.put(FlightCompanyName,“SpiceJet”);
地图放置(航班号,“SG-123”);
地图放置(飞行时间,“6:00-7:00”);
地图放置(飞行停止,“1h 30米不停止”);
map.put(飞行成本,12000卢比);
//将哈希列表添加到ArrayList
flightData.add(地图);
}
onewayListView=(ListView)findViewById(R.id.LV离开);
ReturnListView=(ListView)findViewById(R.id.lvArrivals);
//通过传递xml数据ArrayList获取适配器
onewaydata=新的OneWayFlightResult(此为flightData);
setAdapter(onewaydata);
returndata=新的ReturnFlightResult(此为flightData);
ReturnListView.setAdapter(returndata);
}
再解释一下,这个设计是为了航班结果。因此,如果用户正在搜索往返。我将向他显示两个结果,一个在左侧,另一个在右侧。现在,我希望用户可以从左侧列表中选择一个航班,从右侧选择一个航班。一旦他选择了航班,我就必须获得所选航班的数据。那么如何选择呢我可以这样做吗。请帮助我检查SparseBooleanArray;
SparseBooleanArray checked;
lvDialog.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
checked = lvDialog.getCheckedItemPositions();
if(checked.get(arg2))
{
chkText.setTextColor(Color.CYAN);
colorRow ++;
}
else
{
chkText.setTextColor(Color.BLACK);
colorRow--;
}
}
});
lvDialog.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
checked=lvDialog.getCheckedItemPositions();
如果(选中。获取(arg2))
{
chkText.setTextColor(Color.CYAN);
colorRow++;
}
其他的
{
chkText.setTextColor(Color.BLACK);
彩色行--;
}
}
});
嘿,这里有一些代码…试试看我用u-r代码在不同的列表视图上显示了1,2,3,4。我将如何获取文本视图中该行的值StonItemClickListener值已经设置好了,我想获取单击中特定行的值。我有一个结果页面,我设置了文本视图中的所有值,图像。listView中设置了所有内容。listView中有多行。因此,我想在listView中获取所选行的数据。你能在聊天中解释我吗