我正在開發一個應用程序,它從Web服務獲取數據並將其顯示在列表視圖中。我正在使用自定義列表視圖適配器。在列表的一行中有兩個按鈕。我想顯示一個對話框,當一個按鈕click.and也當一個對話框按鈕按下時,我想對它做一些操作。在自定義列表視圖項中單擊的對話框
這裏是我的適配器類:
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, DialogCreatorInterface di) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
this.mContext = ctx;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public interface DialogCreatorInterface{
public void showDialog();
}
DialogCreatorInterface dialogCreatorInterface = new DialogCreatorInterface() {
@Override
public void showDialog() {
//Create and show the dialog code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Your Title");
// 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();
}
})
.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();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.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);
TextView firstname = (TextView) vi.findViewById(R.id.fname);
TextView lastname = (TextView) vi.findViewById(R.id.lname);
TextView startTime = (TextView) vi.findViewById(R.id.stime);
TextView endTime = (TextView) vi.findViewById(R.id.etime);
TextView date = (TextView) vi.findViewById(R.id.blank);
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);
//Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
//dialogCreatorInterface.showDialog();
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
Intent zoom=new Intent(parent.getContext(), Profile.class);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
@Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
我曾嘗試一些有dialoginterface..but我couldnt't處理這件事......請有人幫助我..
編輯
這是我Getview方法目前
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
TextView firstname = (TextView) vi.findViewById(R.id.fname);
TextView lastname = (TextView) vi.findViewById(R.id.lname);
TextView startTime = (TextView) vi.findViewById(R.id.stime);
TextView endTime = (TextView) vi.findViewById(R.id.etime);
TextView date = (TextView) vi.findViewById(R.id.blank);
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);
//Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialogBuilder.show();
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
Intent zoom=new Intent(parent.getContext(), Profile.class);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
刪除該接口的東西。只需將showDialogue邏輯作爲現在在界面中的單獨方法編寫即可。並在按鈕被點擊時調用該方法。您已經編寫了onClick事件。 –
我已經嘗試過你所說的..但有同樣的問題:( –
刪除AlertDialog alertDialog = alertDialogBuilder.create();和alertDialog.show();。只需使用alertDialogBuilder.show();它將工作 –