Reputation: 41
I'm working on a piece of code that will show an alertdialog whenever clicked and it will shows a clickable list of string (Which I saved in stringList),
I want user can only choose one of the option, I've tried to find the answer on the internet and most of the tutorial only shows how to make a checkbox where we can choose more than one options.
Then I tried this :
else {
CharSequence cs[] = myList.toArray(new CharSequence[countReceiver]);
AlertDialog.Builder builder = new AlertDialog.Builder(ViewTaskToDoList.this);
builder.setTitle("Choose Member : ");
builder.setItems(cs, new DialogInterface.OnClickListener() {
@Override
public void onClick(
//Will get the value of the clicked option;
}});
builder.show();
}
But this code has a bug, it does show string I saved in stringList (which has been converted to CharSequence), but whenever I rechoose the button, it will doubled the option,
Example :
1st Clicked - Dialog appears with options of A, B
2nd Clicked - Dialog appears with options of A, B, A, B.
Could you guys please help me to find the solution here?
Really appreciate it,
Thank you in advance
Upvotes: 0
Views: 112
Reputation: 4661
As per my understanding from your question. You want to show an list of option which should not having checkbox but with Multiple Selection Option.
If so you need to make all at runtime,I will give you an sample try with it, Step 1: Create layout for AlertDialog with
<ScrollView
android:id="@+id/sc_dynamic_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:id="@+id/mScrollTerminal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Step 2:In Activity
mTerminalLayout = (LinearLayout) findViewById(R.id.mScrollTerminal);
Step 3: Use this code to add the items to the LinearLayout ViewGroup
private void loadTotalTime() {
// TODO Auto-generated method stub
final String[] array = getResources().getStringArray(R.array.cooking_time);
if(array.length != 0 ){
List<String> beans = new ArrayList<String>(selectedTotalTimeItem.values());
mTextItem = new TextView[array.length];
mView = new View[array.length];
for(int i=0;i<array.length;i++){
mLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLayoutParamsView = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
mView[i] = new View(this);
mView[i].setBackgroundColor(Color.GRAY);
mView[i].setLayoutParams(mLayoutParamsView);
mTextItem[i] = new TextView(this);
mTextItem[i].setLayoutParams(mLayoutParams);
mTextItem[i].setTextColor(getResources().getColor(R.color.black));
mTextItem[i].setPadding(10, 15, 20, 15);
mTextItem[i].setSingleLine();
mTextItem[i].setTextSize(15);
if(typePTS55F != null)
mTextItem[i].setTypeface(typePTS55F,Typeface.BOLD);
mTextItem[i].setTag(String.valueOf(i));
mTextItem[i].setGravity(Gravity.CENTER_VERTICAL);
if(beans != null){
for(int j=0;j<beans.size();j++){
if(beans.get(j).equalsIgnoreCase(array[i])){
mTextItem[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tick_blue_icon, 0);//
}
}
}
mTextItem[i].setText(array[i]+" "+getResources().getString(R.string.minutes));//mSetPickUpTerminal.get(i).get("description")
mTerminalLayout.addView(mTextItem[i]);
mTerminalLayout.addView(mView[i]);
}
}
if(mTextItem != null){
//selectedMealTYpeItem.clear();
for(int i=0;i<mTextItem.length;i++){
mTextItem[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Before Selecetd Item", v.getTag()+""+selectedTotalTimeItem);
mSelecetdItem = (String) v.getTag();
Log.i("Selecetd Item", mSelecetdItem);
if(selectedTotalTimeItem.containsKey(Integer.parseInt((String) v.getTag()))){
Log.i("hashMap","ContainsKey");
selectedTotalTimeItem.remove( Integer.parseInt((String) v.getTag()));
mTextItem[Integer.parseInt(mSelecetdItem)].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}else{
mTextItem[Integer.parseInt((String)v.getTag())].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tick_blue_icon, 0);;
selectedTotalTimeItem.put( Integer.parseInt((String) v.getTag()), array[(Integer.parseInt(mSelecetdItem))]);
}
Log.i("After Selecetd Item "," "+ selectedTotalTimeItem);
}
});
}
}
}
Step 4: After finished your item selection to get the result of selected items,
List<String> beans = new ArrayList<String>(selectedTotalTimeItem.values());
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<beans.size();i++){
stringBuilder.append(beans.get(i)+",");
}
if(stringBuilder.length()>0){
category = stringBuilder.substring(0, stringBuilder.length()-1);
}
I know its little difficult to understand but spend some time and try to understand.If you have any doubts please let me know.I am here to help you...
Upvotes: 1