Answer the question
In order to leave comments, you need to log in
How to call the description corresponding to the list item in the AlertDialog from the ListView?
Started making the first app for Android. Faced such a problem: I want that when I click on an element of the list, AlertDialog was called with a description of this element. Each element has its own Title, icon, message (description), or replace message with textview. Help me please. It is clear that you need to drive it all into an array somehow, it’s not clear how :) AlertDialog call for one element (I call it when clicking on the TextView):
public void onShowDialog(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(ShotDrinks.this);
builder.setTitle("Название")
.setMessage("Какое-то описание")
.setCancelable(true)
.setIcon(R.drawable.ic_launcher)
.setNegativeButton("Закрыть", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton ("Поделиться", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "_SUBJECT_");
intent.putExtra(Intent.EXTRA_TEXT, "_BODY_");
startActivity(Intent.createChooser(intent, getString(R.string.app_name)));
}
});
AlertDialog alert = builder.create();
alert.show();
}
Answer the question
In order to leave comments, you need to log in
public class ShotDrinks extends ActionBarActivity {
String title[] = new String[]{"B-52","Mexican Green","Werewolf","Out","BMW"};
String description[] = new String[]{"Baileys, Kahlua, Triple Sec","Tequila, Lemon Juice, Pisan","lalalalal","Whiskey, Cognac, Vodka, Sambuca","Tequila, Triple Sec, Sambuca" };
int[] icons = new int [] {R.drawable.b52, R.drawable.green_mexicano, R.drawable.oboroten, R.drawable.ayt, R.drawable.bmw};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shot_drinks);
// create adapter
ArrayAdapter adapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, title);
// set the adapter to the list list
. setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
onShowDialog(title[position], description[position], icons[position]) ;
}
});
}
public void onShowDialog(String title, String message, int drawable){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title)
.setMessage(message)
.setCancelable(true)
.setIcon(drawable)
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog. cancel();
}
})
.setPositiveButton("Share", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "_SUBJECT_");
intent.putExtra(Intent.EXTRA_TEXT, "_BODY_");
startActivity(Intent.createChooser(intent, getString(R.string.app_name)));
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Approximately it will look like this:
ListView (which will replace TextView) displays data from the collection or list of elements passed to it. Therefore, you will need to create a class for the elements, like:
class CocktailItem{
String title;
String description;
Bitmap icon;
}
AdapterView.OnItemClickListener() {
@Override
public
void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
CocktailItem item = (CocktailItem) parent.getAdapter().getItem(position);
AlertDialog.Builder builder = new AlertDialog.Builder(ShotDrinks.this);
builder.setTitle(item.title)
.setMessage(item.description)
.setCancelable(true)
.setIcon(R.drawable.ic_launcher)
...
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question