gpt4 book ai didi

java - 具有自定义 ListView 的警报对话框

转载 作者:行者123 更新时间:2023-12-02 03:51:43 25 4
gpt4 key购买 nike

我想创建一个带有自定义 ListView 的警报对话框。为此,我为 ListView 创建了自定义布局和适配器。我想要带有单选项目的警报对话框。所以我为对话框设置了一个适配器。现在我想从数组列表中获取所选项目?

  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();
}

自定义警报适配器

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;
}

}

最佳答案

which 参数表示所选项目的索引。所以你可以写:

TimeTable selectedItem = allTables.get(which);

另外,我认为当你调用setSingleChoiceItem方法时,不需要为对话框提供自定义 View 。

这是一个带有字符串数组的工作示例:

AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Dialog").setSingleChoiceItems(new String[]{"Item 1", "Item 2"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
//Item 1 was selected
break;
case 1:
//Item 2 was selected
break;
}
dialog.dismiss();
}
}).create();
dialog.show();

您可以使用ListAdapter代替字符串数组。

关于java - 具有自定义 ListView 的警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com