enter image description here我的问题是我不想两次选择相同的选项.. 当它第一次被点击时应该被禁用。稍后当我们选择其他项目时,应启用禁用项目
public class DialogSelection extends DialogFragment实现了View.OnClickListener {
private static DialogSelection dialogSelection;
private MainActivity mainActivity;
public static DialogSelection newInstance(MainActivity mainActivity) {
if (dialogSelection == null) {
dialogSelection = new DialogSelection();
}
dialogSelection.mainActivity = mainActivity;
return dialogSelection;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] items = {"On Duty", "Off Duty", "Sleeper", "Driving"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Selection")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("Dialogos", "Option Selected: " + items[item]);
switch (items[item]) {
case "On Duty":
mainActivity.updateTimelineStatus(DriverState.STATE_ONLINE);
break;
case "Off Duty":
mainActivity.updateTimelineStatus(DriverState.STATE_OFFLINE);
break;
case "Sleeper":
mainActivity.updateTimelineStatus(DriverState.STATE_SLEEPER);
break;
case "Driving":
mainActivity.updateTimelineStatus(DriverState.STATE_DRIVING);
break;
default:
break;
}
}
});
return builder.create();
}
@Override
public void onClick(View v) {
}
}
答案 0 :(得分:0)
你是否使用string []替换列表
List<String> list = new ArrayList<String>(Arrays.asList(str_array));
list.add("onduty");
...
add ur need options
//check is string variable create inside ur class
if(check!=null)
{
list.remove("check")
}
答案 1 :(得分:0)
刚写完,你可以尝试一下。
private final String[] items = {"On Duty", "Off Duty", "Sleeper", "Driving"};//your data
private ItemAdapter itemAdapter;//adapter
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (itemAdapter.getSelectPosition() == i) {
return;
}
alertDialog.dismiss();
itemAdapter.select(i);
//your code
Log.i("Dialogos", "Option Selected: " + items[i]);
switch (items[i]) {
case "On Duty":
break;
case "Off Duty":
break;
case "Sleeper":
break;
case "Driving":
break;
default:
break;
}
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (itemAdapter == null) {
itemAdapter = new ItemAdapter(getActivity(), items, -1);
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Selection");
final AlertDialog alertDialog = builder.create();
ListView listView = new ListView(getActivity());
alertDialog.setView(listView);//use custom ListView
listView.setAdapter(itemAdapter);//user custom Adapter
listView.setOnItemClickListener(onItemClickListener);
return alertDialog;
}
/**
* Custom Adapter
*/
private class ItemAdapter extends BaseAdapter {
private String[] data;
private LayoutInflater layoutInflater;
private int selectPosition = -1;//disabled item position
public void select(int selectPosition) {
this.selectPosition = selectPosition;
}
public int getSelectPosition() {
return selectPosition;
}
public ItemAdapter(Context contexts, String[] data, int selectPosition) {
layoutInflater = LayoutInflater.from(contexts);
this.data = data;
this.selectPosition = selectPosition;
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int i) {
return data[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView textView = (TextView) layoutInflater.inflate(android.R.layout.simple_expandable_list_item_1, null);//use system default layout
textView.setPadding(20, 20, 20, 20);
textView.setText(data[i]);//set data
textView.setEnabled(this.selectPosition != i);//disable or enable
return textView;
}
}