Add radioButton in ListView
I need to add RadioButton in ListView and indeed I have tried, But the problem that I am getting is that when I click on radio button it get selected wonderfully... but the same time if wish not to select that particular button... it just remains selected. Here I am providing my code:
import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class RadioInCustomListView extends ListActivity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] names = new String[] { "Issues", "Request for Information",
"Contracts", "Purchase Orders", "Change Orders", "Proposals",
"Submittals" };
lv = getListView();
lv.setCacheColorHint(Color.TRANSPARENT);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.radio_item, names);
//this.setListAdapter (new ArrayAdapter<String>(RadioInCustomListView.this,R.layout.radio_item, names));
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
Toast.makeText(RadioInCustomListView.this, o.toString(), Toast.LENGTH_LONG).show();
}
}
My xmls: main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+android:id/list"
开发者_JAVA技巧 android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
radio_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
may be this code help you
ls.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,list_sexuality));
ls.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
精彩评论