如何在 toast 消息中成功显示我选择的列表视图项?
How do I successfully show my selected listview item in a toast message?
我一直在修补一些来自 udacity 的代码。
https://github.com/udacity/ud839_CustomAdapter_Example
我想在另一个 activity 中使用我从列表中选择的内容。我通常使用 toast 消息来测试我是否成功选择了正确的项目等。但即使是我的 toast 消息也会使应用程序崩溃。
下面是 arraylist、listview 和 onitemclicklistener 的简化版本。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ArrayList of AndroidFlavor objects
final ArrayList<AndroidFlavor> androidFlavors = new ArrayList<AndroidFlavor>();
androidFlavors.add(new AndroidFlavor("Lollipop", "5", R.drawable.lollipop));
androidFlavors.add(new AndroidFlavor("Nougat", "7", R.drawable.nougat));
androidFlavors.add(new AndroidFlavor("Oreo", "8", R.drawable.oreo));
// Create an {@link AndroidFlavorAdapter}, whose data source is a list of
// {@link AndroidFlavor}s. The adapter knows how to create list item views for each item
// in the list.
final AndroidFlavorAdapter flavorAdapter = new AndroidFlavorAdapter(this, androidFlavors);
// Get a reference to the ListView, and attach the adapter to the listView.
ListView listView = (ListView)findViewById(R.id.listview_flavor);
listView.setAdapter(flavorAdapter);
下面是我认为导致问题的代码,如果我手动传递字符串,onItemClick 可以工作,但它不会传递选定的列表项。 androidFlavors.get(position) 似乎不正确,但我不确定为什么。
//onClickItemListener to take selected Arraylist component and display the selected component in a toast message
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
Toast.makeText(getApplicationContext(), (CharSequence) androidFlavors.get(position), Toast.LENGTH_SHORT).show();
}
});
}
}
您正在尝试将 new AndroidFlavor("Nougat", "7", R.drawable.nougat)
转换为 CharSequence
。相反,您应该使用 getVersionName
。
Toast.makeText(getApplicationContext(), androidFlavors.get(position).getVersionName(), Toast.LENGTH_SHORT).show();
我一直在修补一些来自 udacity 的代码。
https://github.com/udacity/ud839_CustomAdapter_Example
我想在另一个 activity 中使用我从列表中选择的内容。我通常使用 toast 消息来测试我是否成功选择了正确的项目等。但即使是我的 toast 消息也会使应用程序崩溃。
下面是 arraylist、listview 和 onitemclicklistener 的简化版本。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ArrayList of AndroidFlavor objects
final ArrayList<AndroidFlavor> androidFlavors = new ArrayList<AndroidFlavor>();
androidFlavors.add(new AndroidFlavor("Lollipop", "5", R.drawable.lollipop));
androidFlavors.add(new AndroidFlavor("Nougat", "7", R.drawable.nougat));
androidFlavors.add(new AndroidFlavor("Oreo", "8", R.drawable.oreo));
// Create an {@link AndroidFlavorAdapter}, whose data source is a list of
// {@link AndroidFlavor}s. The adapter knows how to create list item views for each item
// in the list.
final AndroidFlavorAdapter flavorAdapter = new AndroidFlavorAdapter(this, androidFlavors);
// Get a reference to the ListView, and attach the adapter to the listView.
ListView listView = (ListView)findViewById(R.id.listview_flavor);
listView.setAdapter(flavorAdapter);
下面是我认为导致问题的代码,如果我手动传递字符串,onItemClick 可以工作,但它不会传递选定的列表项。 androidFlavors.get(position) 似乎不正确,但我不确定为什么。
//onClickItemListener to take selected Arraylist component and display the selected component in a toast message
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
Toast.makeText(getApplicationContext(), (CharSequence) androidFlavors.get(position), Toast.LENGTH_SHORT).show();
}
});
}
}
您正在尝试将 new AndroidFlavor("Nougat", "7", R.drawable.nougat)
转换为 CharSequence
。相反,您应该使用 getVersionName
。
Toast.makeText(getApplicationContext(), androidFlavors.get(position).getVersionName(), Toast.LENGTH_SHORT).show();