Developer
Developer

Reputation: 6350

ListView Selction in Android?

I have 2 ListView in my Layout and one common row layout for it and the row is repeating multiple times .I am confused how i will identify on both of ListView which result is selected and how i will get the data of TextViews that are selected in both the listViews.

This is my ListView XML

<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>

And this is the Row Layout that i am putting in these two ListViews

<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>

And in MY 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);
    }

A bit more explanation this design is for flight result.So if the user is searching for roundtrip.I will show him 2 result one on left and other on right side .Now what i want that user can select one flight from left list and and one flight from the right side.Once he select the flight .I have to get the data of the flight selected.So how could i do this .Please help me

Upvotes: 0

Views: 74

Answers (1)

Looking Forward
Looking Forward

Reputation: 3585

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

hey here is some code...try

Upvotes: 1

Related Questions