I am trying to create my own list view. Each list entry will have an image, a text image and a radio object. I finally made a list, but now I have a problem with the selection of radio buttons.
I want that every time the user clicks anywhere in the record, he must set the corresponding radio button and automatically deselect other radio buttons in the list.
Here is my code: bus_item.xml: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imglist_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:src="@drawable/alaknanda" />
<TextView
android:id="@+id/txtlist_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Alaknanda"
android:textSize="22sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/radio_list"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
BusListActivity.java: -
package com.hpubts50.hpubustrackerserver;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
public class BusListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_list);
setListAdapter(new MyBusListAdapter(this, android.R.layout.simple_list_item_1, R.id.txtlist_name, getResources().getStringArray(R.array.HPU_Buses)));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.e("MYTAG",""+position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bus_list, menu);
return true;
}
private class MyBusListAdapter extends ArrayAdapter<String> {
public MyBusListAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("MYTAG","one");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.bus_item, parent, false);
String[] items = getResources().getStringArray(R.array.HPU_Buses);
Log.e("MYTAG","two");
ImageView imglist_icon = (ImageView) row.findViewById(R.id.imglist_icon);
TextView txtlist_name = (TextView) row.findViewById(R.id.txtlist_name);
RadioButton radio_list = (RadioButton) row.findViewById(R.id.radio_list);
Log.e("MYTAG","three");
txtlist_name.setText(items[position]);
Log.e("MYTAG","four");
if (items[position].equals("Alakananda")) {
imglist_icon.setImageResource(R.drawable.alaknanda);
} else if (items[position].equals("Alakananda")) {
imglist_icon.setImageResource(R.drawable.alaknanda);
}else if (items[position].equals("Chetanya")) {
imglist_icon.setImageResource(R.drawable.chetanya);
}else if (items[position].equals("Garuda")) {
imglist_icon.setImageResource(R.drawable.garuda);
}else if (items[position].equals("Nandi")) {
imglist_icon.setImageResource(R.drawable.nandi);
}else if (items[position].equals("Neela")) {
imglist_icon.setImageResource(R.drawable.neela);
}
Log.e("MYTAG","five");
return row;
}
}
}
activity_bus_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BusListActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>