我有ListView
使用customlistadapter
的自定义适配器。如果点击了该项目或行,则会显示AlertDialog
negative
和positive
按钮。现在我希望positive
中的每个AlertDialog
按钮启动不同的活动。
例如:如果我从列表/第一行的对话框中单击肯定按钮,它将显示Activity1,如果我在列表/第二行的对话框中单击肯定按钮,它将显示Activity2。
这是我的MenuList.java
:
public class MenuList extends ActionBarActivity{
ListView list;
String[] itemname ={
"RM. Kemang raya",
"Granny's Nest",
"RM. Dua Saudara",
"Solaria Giant",
"Rumah Kayu",
"RM. Mbok Wito 2",
};
String[] itemdescription ={
"[Umum]\nJl.Endro Suratmin Sukarame, Bandar Lampung.\nTelp.0721723732",
"[Cafe]\nJl.P.Antasari Gg.P.Sebuku Sukarame, Bandar Lampung.\nTelp.0821 8619 8999",
"[Masakan Padang]\nJl.P.Tirtayasa Sukabumi, Bandar Lampung.\nTelp.0721745627",
"[Umum/Cafe]\nJl.P.Antasari Sukarame, Bandar Lampung.\nTelp.0721 773066",
"[Umum]\nJl.Arief Rahman Hakim No. 45, Bandar Lampung.\nTelp.0721 700666",
"[Umum]\nJl.Arief Rahman Hakim Blok A No.16, Bandar Lampung.\nTelp.0721 701298",
};
Integer[] imgid={
R.drawable.ic_listrestro,
R.drawable.ic_listrestro,
R.drawable.ic_listrestro,
R.drawable.ic_listrestro,
R.drawable.ic_listrestro,
R.drawable.ic_listrestro,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_restoran);
// enable up/back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MenuRestoran.this, MapsActivity.class);
startActivity(intent);
}
});
CustomListAdapter adapter=new CustomListAdapter(this, itemname, itemdescription, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
//alert dialog
AlertDialog.Builder adb = new AlertDialog.Builder(
MenuRestoran.this);
adb.setTitle(itemname[+position]);
adb.setMessage("Lihat Lokasi di Map?");
adb.setIcon(imgid[+position]);
adb.setPositiveButton("Lihat", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MenuRestoran.this, MapsRMKemang.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
adb.setNegativeButton("Batal", null);
adb.show();
}
});
}
这是我的CustomListAdapter.java
:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final String[] itemdescription;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, String[] itemdescription, Integer[] imgid) {
super(context, R.layout.list_view, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
this.itemdescription = itemdescription;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_view, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView Descriptiontxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
Descriptiontxt.setText(itemdescription[position]);
imageView.setImageResource(imgid[position]);
return rowView;
};
你们可以告诉我这样做吗?感谢
答案 0 :(得分:0)
在你的适配器中,
public class CustomListAdapter extends ArrayAdapter<String> {
private ClickCallback clickCallback;
public void setClickCallback(ClickCallback clickCallback) {
this.clickCallback = clickCallback;
}
getView
中的
rowView.setOnClickListener(new View.OnClickListner() {
@Override
public void onClick(View v) {
if (clickCallback == null) {
return;
}
clickCallback.invoke(position);
}
};);
您可以在正面和负面按钮上附加听众。
此处clickCallback
是您需要实现的自定义类来处理回调。
public interface ClickCallback {
public void invoke(int position);
}
在您的活动中,
private class PositiveCallback implements ClickCallback {
public void invoke(int position) {
// call your activities
}
}
CustomListAdapter adapter=new CustomListAdapter(this, itemname, itemdescription, imgid);
adapter.setClickCallback(new PositiveCallback());
答案 1 :(得分:0)
您已拥有大部分代码,只需添加对position
启动AlertDialog
时点击的final int pos
的检查。
您必须添加onClickListener()
,以便可以在setPositiveButton()
的内部 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
final int pos = position; //added
//alert dialog
AlertDialog.Builder adb = new AlertDialog.Builder(
MenuRestoran.this);
adb.setTitle(itemname[+position]);
adb.setMessage("Lihat Lokasi di Map?");
adb.setIcon(imgid[+position]);
adb.setPositiveButton("Lihat", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//New Code below:
if (pos == 0) {
Intent intent = new Intent(MenuRestoran.this, Activity1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
else if (pos == 1) {
Intent intent = new Intent(MenuRestoran.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
//end new code.....
}
});
adb.setNegativeButton("Batal", null);
adb.show();
}
});
中访问它:
.destroy