我有一个对话框类,通过从MainActivity调用addDialog()来创建一个带有listview的对话框。
public class LanDevicesDialog
{
public static Dialog dialog;
public ArrayList<String> array = new ArrayList<String>();
LanDeviceListAdapter adapter = new LanDeviceListAdapter(MainActivity.mContext, array);
public void addDialog(Context ctx)
{
dialog = new Dialog(ctx);
dialog.setContentView(R.layout.add_lan_device_dialog);
dialog.show();
ListView listView = (ListView) dialog.findViewById(R.id.lan_device_list);
listView.setAdapter(adapter);
}
public void updateDialog(String item)
{
array.add(item);
adapter.notifyDataSetChanged();
}
public static class LanDeviceListAdapter extends BaseAdapter
{...}
}
我还有一台服务器,它向我发送我想要在每个响应中添加到listview的数据。
例如,对话框打开,服务器发送数据,所以listview有一个项目。服务器再次发送数据,以便listview中有2个项目。
在服务器响应上,我调用AsyncTask,它调用onPostExecute()中的updateDialog()方法。
protected void onPostExecute(Void aVoid)
{
LanDevicesDialog devicesDialog = new LanDevicesDialog();
devicesDialog.updateDialog("test");
super.onPostExecute(aVoid);
}
问题是列表视图根本没有显示任何内容。
答案 0 :(得分:0)
像这样构建对话
AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(YourActivity.this);
dialogBuilder.setTitle("Choose any");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String
(YourActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("One");
arrayAdapter.add("Two");
arrayAdapter.add("Three");
arrayAdapter.add("Four");
arrayAdapter.add("Five");
dialogBuilder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new AlertDialog.Builder(YourActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Your Selected Item is");
builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
builderInner.show();
}
});
dialogBuilder.show(); `