Answer the question
In order to leave comments, you need to log in
How can I make it so that when I click on an item in the ListView, a dialog opens with the information I need?
Good day to all!
Being completely green in Android development, I encountered a problem for which I could not find a solution on the Internet ( If you still have a link or a ready-made solution, I will be grateful. ).
There is a string array with the name of coffee drinks, which is passed to the ListView .
By clicking on the element, an AlertDialog opens , and in it I want to pass the composition of these drinks.
Here is the code:
public class MainActivity extends AppCompatActivity {
final Context context = this;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);//листвью
final String[] values = new String[]
{"Americano", "Cappuccino", "Latte"};//строковый массив
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);//создаю адаптер, переменная values содержит название напитков
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//обработчик нажатия на элемент в листвью
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPosition = position;
String itemValue = (String) listView.getItemAtPosition(position);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Message:");
alertDialogBuilder.setMessage( "Название напитка:\n" + itemValue);//здесь должен показываться состав вроде молоко, объем и т.д.
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();//вывожу диалог на экран
}
});
}
}
Answer the question
In order to leave comments, you need to log in
More shortly begin that throw out ListView. Forget about it like it doesn't exist. He teaches bad things, all these ArrayAdapters are evil, you can't see behind them how to do it right.
Take RecyclerView. It will seem harder to you, but it will make you do the right thing.
Get away from the string list. The list of strings is nothing, the string "espresso" has no composition. You must have a model, a POJO, that stores your data for one list item. Is it one line in your case? Great, this will be a class with one field. Then you will add one more field to the same class - id (id) of this element. By ID, you should be able to unambiguously receive both the name and the list of ingredients.
Next, when you write a custom adapter for RecyclerView, you will need to put onClickListener on the views. pressing this listener you have to throw into your interface
interface CoffeeSelectionListener {
void onCoffeeSelected(int coffeeId);
}
Well, in a nutshell: Java has such a Map data structure - it stores lists in the form of a key, value.
For example:
(Americano)(Coffee, Water)
(Cappuccino)(Coffee, Milk)
Create such a dictionary. When the user clicks on a cappuccino, use the get("cappuccino") method to retrieve a list of ingredients from this map.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question