作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个应用程序,它从网络服务中获取一些数据并显示在 ListView 中。我已经实现了一个由 BaseAdapter 扩展的自定义适配器。在 getView()
方法中,我也对原始文件进行充气。这些工作正常。
我的问题是我已经实现了当用户单击列表项时显示对话框的代码,但现在我想显示另一个对话框,其中有一个自定义列表(当单击"is"按钮时)。我还想在该 ListView 中显示一些数据。 [我有一个 ArrayList,里面装满了我想要的数据]。我正在我的适配器类中编写代码。任何人都可以告诉我如何去做吗?
这是我的代码:
public class NewsRowAdapter extends BaseAdapter {
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
this.mContext = ctx;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void dialogshow(final String Date,final String Start,final String End){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
//MainActivity.this.finish();
// Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
//check similer records
// ShortList sh = new ShortList();
// ArrayList<HashMap<String, String>> duplicateList;
// duplicateList=sh.getDuplicated(Date, Start, End);
//if duplicates > 1 then show the popup list
// if(duplicateList.size()>1){
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
LayoutInflater infl = activity.getLayoutInflater();
//View vi = infl.inflate(id, root)
alertDialogBuilder2.setView(infl.inflate(R.layout.dialog_row, null))
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialogBuilder2.show();
// }
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
final TextView firstname = (TextView) vi.findViewById(R.id.fname);
final TextView lastname = (TextView) vi.findViewById(R.id.lname);
final TextView startTime = (TextView) vi.findViewById(R.id.stime);
final TextView endTime = (TextView) vi.findViewById(R.id.etime);
final TextView date = (TextView) vi.findViewById(R.id.blank);
final ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
endTime.setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_DATE));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
Button accept = (Button) vi.findViewById(R.id.button1);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int x = (int) getItemId(position);
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
// get the intent from the hashmap check if there is similar date and time.
//then store them in a list or array.
String getDate = (String) date.getText();
String getStartTime = startTime.getText().toString();
String getEndTime = endTime.getText().toString();
dialogshow(getDate,getStartTime,getEndTime);
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getFname = firstname.getText().toString();
Toast.makeText(parent.getContext(), "view clicked: "+getFname , Toast.LENGTH_SHORT).show();
//get the id of the view
//check the id of the request
//call the web service acording to the id
Intent zoom=new Intent(parent.getContext(), Profile.class);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
最佳答案
你在正确的轨道上,这是我用来动态显示其中包含项目列表的对话框的方法。
作为引用,这里提出了一个类似的问题:Android custom list dialog
//String[] list_data; Preloaded with a String array
final CharSequence[] items = new CharSequence[list_data.length];
for (int i = 0; i < list_data.length; i++) {
items[i] = list_data[i];
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select the data you want");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get the id of the item
diag_callback();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void diag_callback() {
//Do someting when the user has made his selection
}
希望这能解决您的问题。
关于android - 如何将自定义 ListView 设置为对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20371218/
我是一名优秀的程序员,十分优秀!