如何设置自定义列表视图在对话框中 [英] how to set a custom list view into a dialog box
问题描述
我开发从列表视图Web服务并显示获取一些数据的应用程序。我已经实现了它是由BaseAdapter扩展自定义适配器。在 getView()
方法,我吹生也......这些都可以正常使用。
我的问题是我已经实现了code,显示一个对话框,当用户点击一个列表项,但现在我想说明它有它里面的自定义列表另一个对话框(如果是按钮点击)。我也想显示在列表视图中的一些数据。 [我充满了我想要的数据的ArrayList。我写在我的适配器类的code。任何人都可以给我一些想法如何做到这一点?
这是我的code:
公共类NewsRowAdapter扩展了BaseAdapter {
私人语境mContext;
私人活动的活动;
私有静态LayoutInflater吹气= NULL;
私人的ArrayList< HashMap的<字符串,字符串>>数据;
INT资源;
//字符串响应;
//上下文的背景下;
//初始化适配器
公共NewsRowAdapter(上下文CTX,活动行为,INT资源的ArrayList< HashMap的<字符串,字符串>> D){
超();
this.resource =资源;
this.data = D;
this.activity =行为;
this.mContext = CTX;
充气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
公共无效dialogshow(最后弦乐日期,最后的字符串开始,最后弦乐完){
AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(活动);
alertDialogBuilder.setTitle(确认您的操作!);
//设置对话框消息
alertDialogBuilder
.setMessage(单击是退出!)
.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
//如果单击此按钮,关闭
//当前活动
//MainActivity.this.finish();
// Toast.makeText(mContext,是点击,Toast.LENGTH_LONG).show();
//检查similer记录
//入围SH =新名单();
// ArrayList的< HashMap的<字符串,字符串>> duplicateList;
// duplicateList = sh.getDuplicated(日期,开始,结束);
//如果重复> 1,则显示弹出列表
//如果(duplicateList.size()→1){
AlertDialog.Builder alertDialogBuilder2 =新AlertDialog.Builder(活动);
LayoutInflater INFL = activity.getLayoutInflater();
//查看VI = infl.inflate(ID,根)
alertDialogBuilder2.setView(infl.inflate(R.layout.dialog_row,NULL))
.setPositiveButton(接受,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
Toast.makeText(mContext,接受,Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(取消,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
dialog.dismiss();
}
});
alertDialogBuilder2.show();
//}
}
})
.setNegativeButton(否,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
//如果单击此按钮,只需关闭
//对话框并做什么
dialog.cancel();
}
});
alertDialogBuilder.show();
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,最终的ViewGroup父){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.row,NULL);
最后TextView的名字=(TextView中)vi.findViewById(R.id.fname);
最后TextView的姓氏=(TextView中)vi.findViewById(R.id.lname);
最后的TextView的startTime =(TextView中)vi.findViewById(R.id.stime);
最后的TextView endTime的=(的TextView)vi.findViewById(R.id.etime);
最后的TextView日期=(TextView中)vi.findViewById(R.id.blank);
最后ImageView的IMG =(ImageView的)vi.findViewById(R.id.list_image);
HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
歌= data.get(位置);
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);
按钮接受=(按钮)vi.findViewById(R.id.button1);
accept.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
最终诠释X =(int)的getItemId(位置);
/ *意图变焦=新的意图(mContext,Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(缩放); * /
//从散列映射检查的意图是否存在类似的日期和时间。
//然后将它们存储在一个列表或数组。
字符串GETDATE =(字符串)date.getText();
字符串getStartTime = startTime.getText()的toString()。
。字符串getEndTime = endTime.getText()的toString();
dialogshow(GETDATE,getStartTime,getEndTime);
}
});
vi.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
字符串getFname = firstname.getText()的toString()。
Toast.makeText(parent.getContext(),观后:+ getFname,Toast.LENGTH_SHORT).show();
//获取视图的id
//检查请求的ID
//调用Web服务acording的id
意图变焦=新的意图(parent.getContext(),Profile.class);
。parent.getContext()startActivity(变焦);
}
});
返回六;
}
您是在正确的轨道,这里是我用于动态显示一个对话框,项目中有一个列表中。
有关引用一个类似的问题在这里问: Android的自定义列表对话框
//的String [] list_data; ploaded一个String数组$ P $
最后的CharSequence []项目=新的CharSequence [list_data.length]
的for(int i = 0; I< list_data.length;我++){
项目[i] = list_data [I]
}
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(选择数据你想要的);
builder.setItems(项目,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
//获取项目的ID
diag_callback();
}
});
AlertDialog警报= builder.create();
alert.show();
}
公共无效diag_callback(){
//不治本,当用户做出了他的选择
}
希望这会挑选出你的问题。
I am developing a application which fetches some data from a web service and displays in a list view. I have implemented a custom adapter which is extended by BaseAdapter. In the getView()
method I inflate the raw also.. Those are working perfectly.
My problem is I have implemented code to show a dialog box when user click on an list item, but now I want to show another dialog box which has a custom list inside it (when Yes button clicked). I also want to show some data in that listview. [I have a ArrayList filled with the data that I wanted] . I'm writing the code inside my adapter class. Can anyone give me some idea how to do it ?
This is my code:
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;
}
You are on the right track, Here is what i used to dynamically display a Dialog with a list of items in it.
For reference a similar Question was asked here : 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
}
Hope this sorts out your problem.
这篇关于如何设置自定义列表视图在对话框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!