I have a ListView
with a custom adapter using customlistadapter
. If the item or row is clicked it will show an AlertDialog
with negative
and positive
buttons. Now I want for every positive
button in the AlertDialog
to start a different Activity.
我有一个ListView与自定义适配器使用customlistadapter。如果单击项目或行,它将显示带有负和正按钮的AlertDialog。现在我希望AlertDialog中的每个正面按钮启动一个不同的Activity。
Ex : If I click positive button in the dialog from list/row number one, it will be show Activity1 and if I click positive button in the dialog in list/row number two, it will show Activity2.
例如:如果我从列表/第一行的对话框中单击肯定按钮,它将显示Activity1,如果我在列表/行第二个对话框中单击肯定按钮,它将显示Activity2。
This is my MenuList.java
:
这是我的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();
}
});
}
This is my CustomListAdapter.java
:
这是我的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;
};
have you guys can tell me to do this? Thanks
你们有人能告诉我这样做吗?谢谢
2 个解决方案
#1
0
You already have most of the code, you just need to add a check for the position
that was clicked on that initiated the AlertDialog
.
您已经拥有了大部分代码,只需添加一个检查点击的位置即可启动AlertDialog。
You'll have to add a final int pos
so that it can be accessed in the inner onClickListener()
in setPositiveButton()
:
你必须添加一个最终的int pos,以便可以在setPositiveButton()中的内部onClickListener()中访问它:
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();
}
});
#2
0
In your adapter,
在你的适配器中,
public class CustomListAdapter extends ArrayAdapter<String> {
private ClickCallback clickCallback;
public void setClickCallback(ClickCallback clickCallback) {
this.clickCallback = clickCallback;
}
in getView
rowView.setOnClickListener(new View.OnClickListner() {
@Override
public void onClick(View v) {
if (clickCallback == null) {
return;
}
clickCallback.invoke(position);
}
};);
You may attach listener on positive and negative buttons.
您可以在正面和负面按钮上附加监听器。
Here clickCallback
is your custom class which you need to implement to handle callbacks.
这里clickCallback是您需要实现的自定义类来处理回调。
public interface ClickCallback {
public void invoke(int position);
}
In your activity,
在你的活动中,
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
You already have most of the code, you just need to add a check for the position
that was clicked on that initiated the AlertDialog
.
您已经拥有了大部分代码,只需添加一个检查点击的位置即可启动AlertDialog。
You'll have to add a final int pos
so that it can be accessed in the inner onClickListener()
in setPositiveButton()
:
你必须添加一个最终的int pos,以便可以在setPositiveButton()中的内部onClickListener()中访问它:
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();
}
});
#2
0
In your adapter,
在你的适配器中,
public class CustomListAdapter extends ArrayAdapter<String> {
private ClickCallback clickCallback;
public void setClickCallback(ClickCallback clickCallback) {
this.clickCallback = clickCallback;
}
in getView
rowView.setOnClickListener(new View.OnClickListner() {
@Override
public void onClick(View v) {
if (clickCallback == null) {
return;
}
clickCallback.invoke(position);
}
};);
You may attach listener on positive and negative buttons.
您可以在正面和负面按钮上附加监听器。
Here clickCallback
is your custom class which you need to implement to handle callbacks.
这里clickCallback是您需要实现的自定义类来处理回调。
public interface ClickCallback {
public void invoke(int position);
}
In your activity,
在你的活动中,
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());