0
我想用自定義列表視圖創建警報對話框。爲此,我爲列表視圖創建了自定義佈局和適配器。 我想單選項目的警報對話框。所以我在對話框中設置了一個適配器。現在我想從數組列表中選擇項目?帶自定義列表視圖的警報對話框
selectTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final TimeTable table = new TimeTable();
List<TimeTable> allTables = new ArrayList<TimeTable>();
TimeTableHelper timeTableHelper = new TimeTableHelper(AddEventActivity.this);
allTables = timeTableHelper.getAllTables();
allTables.add(table);
final List<TimeTable> t = th.getAllTables();
for (TimeTable cn : allTables) {
String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
"Status: " + cn.getStatus() + ",Color: " + cn.getTableColor();
Log.d("Data ", log);
}
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);
lv = (ListView) convertView.findViewById(R.id.tableslist);
final CustomAlertAdapter adapter = new CustomAlertAdapter(allTables, AddEventActivity.this);
alertDialog.setView(convertView);
alertDialog.setView(customView);
alertDialog.setSingleChoiceItems(adapter, selected, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final List<TimeTable> tables = new ArrayList<TimeTable>();
// what to do here to get item of TimeTable list?
}
});
alertDialog.show();
}
});
編輯:
alertDialog.setSingleChoiceItems(adapter, selected, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
TimeTable selectedItem = allTables.get(which);
selectedItem.getId();
txtTable.setText(selectedItem.getTitle());
Log.d("TableId", String.valueOf(selectedItem));
dialog.dismiss();
}
});
alertDialog.show();
}
customAlertAdapter
public class CustomAlertAdapter extends BaseAdapter{
List<TimeTable> mData;
Context mContext;
LayoutInflater inflater;
public CustomAlertAdapter(List<TimeTable> data, Context context) {
mData = data;
mContext = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.alertlistrow, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tableTitle);
Button color = (Button)convertView.findViewById(R.id.selectColor);
tvTitle.setText(mData.get(position).getTitle());
((GradientDrawable)color.getBackground()).setColor(mData.get(position).getTableColor());
return convertView;
}
}
項目沒有被點擊。如果我點擊物品,沒有任何反應。 – Sid
請檢查編輯。 @Misagh Emamverdi – Sid
@Sid嘗試從警報對話框中刪除自定義視圖。 –