PS: the reference link ” http://www.androidpeople.com/android-custom-listview-tutorial-part-1″ is not available.
Dears, I am trying to create a single choice ListView accompany with mutilple items list on Android by using the below code (1 java main program and 2 xml layout files). However it will shows multiple radiobuttons enabled, this is not my expection, they should be mutually exclusive !
And How to get the position/content(?) of the choiced listview ?
Could you help me review where I was wrong and how to implement it ? Thanks a lot for the Help !
My code lists:
main java code:
package list.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MultipleItemsListView extends Activity
{
private ListView mListView01;
private SimpleAdapter mSimpleAdapter;
private List<String> allTxt1;
private List<String> allTxt2;
private List<String> allTxt3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView01 = (ListView)this.findViewById(R.id.listView1);
updateListView();
}
private void updateListView()
{
allTxt1 = new ArrayList<String>();
allTxt2 = new ArrayList<String>();
allTxt3 = new ArrayList<String>();
for (int i = 0 ; i<5; i++)
{
allTxt1.add("test1"+i);
allTxt2.add("test2"+i);
allTxt3.add("test3"+i);
}
if(allTxt1.size()>0)
{
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i< allTxt1.size(); i++)
{
map = new HashMap<String, String>();
map.put("txt1", allTxt1.get(i));
map.put("txt2", allTxt2.get(i));
map.put("txt3", allTxt3.get(i));
mylist.add(map);
}
mSimpleAdapter = new SimpleAdapter
(
this, mylist, R.layout.items_list ,
new String[] {"txt1", "txt2", "txt3",""},
new int[] {R.id.textView1, R.id.textView2 , R.id.textView3 ,R.id.radioButton1}
);
mListView01.setItemsCanFocus(true);
// mListView01.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView01.setAdapter(mSimpleAdapter);
}
}
}
Layout1:
main.xml
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RadioGroup> </LinearLayout>
Layout2:
items_list.xml
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:text="TextView1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:text="TextView2" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:text="TextView3" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip"/> </LinearLayout>
It seems hard to get the solution/response from our friends. And I had to let progress to go.
So, I used an alternative to cover this question/problem, and I would like share it below:
I combinded every list items’ description(text String) to One String first. And then, implements the “simple_list_item_single_choice.xml” with this String to build the ListView.
To use this way there are some limitation for my applicaiton, is not the best way for me (only 60% meet my expection.). Hope there is correct method responsed !