[英]How to display text for an item in a list view Android Studio
I'm having a lot of trouble figuring this out. 我很难解决这个问题。 What I want to do is display a different description depending on which item I click from a list view.
我要做的是根据从列表视图中单击的项目显示不同的描述。 Let's say I choose the third recipe on the list.
假设我选择列表中的第三个食谱。 It should switch to another activity and show the description for that recipe only and not any other recipe.
它应该切换到另一个活动,并仅显示该配方的描述,而不显示任何其他配方。 I have the descriptions in the strings folder.
我在字符串文件夹中有描述。
In my code, it has a listener and an intent to open the other activity. 在我的代码中,它具有侦听器和打开其他活动的意图。 When I run the app, once I click on the item on the list, it closes.
当我运行该应用程序时,一旦单击列表中的项目,它就会关闭。 Before, I did not use a listener, but a method and have an onClick for the textview and image view in the xml.
以前,我没有使用侦听器,而是使用了一个方法,并且在xml中具有onClick的textview和image视图。 That would switch the activity, but would not show any description.
这将切换活动,但不会显示任何描述。
public class MainActivity extends AppCompatActivity {
private RecipesDataSource ds;
private ListView recipesListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ds = new RecipesDataSource();
recipesListView = (ListView)findViewById(R.id.listView1);
recipesListView.setAdapter(new RecipesDataSourceAdapter(this,ds));
recipesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(MainActivity.this, RecipesInfoActivity.class);
i.putExtra("recipe_desc", position);
startActivity(i);
}
});
}
}
Here is the second activity where I want the description to appear. 这是我希望描述出现的第二个活动。 It has a text view for holding the text.
它具有用于保存文本的文本视图。
public class RecipesInfoActivity extends AppCompatActivity {
TextView recipeInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recipeInfo = (TextView)findViewById(R.id.recipeInfoTextView);
Intent i = getIntent();
String pos = i.getStringExtra("recipe_desc");
recipeInfo.setText(writeDesc(pos));
}
public int writeDesc(String pos)
{
int position = Integer.parseInt(pos);
int desc = 0;
if(position==0)
{
desc = R.string.description1;
}
else if(position==1)
{
desc = R.string.description2;
}
else if(position==2)
{
desc = R.string.description3;
}
else if(position==3)
{
desc = R.string.description4;
}
else if(position==4)
{
desc = R.string.description5;
}
else if(position==5)
{
desc = R.string.description6;
}
else if(position==6)
{
desc = R.string.description7;
}
return desc;
}
}
The method writeDesc()
returns the description depending on the position that putExtra()
passes from the MainActivity
. 方法
writeDesc()
根据putExtra()
从MainActivity
传递的位置返回描述。 The variables are ints because I'm using an ArrayList
with Integers. 变量是整数,因为我正在使用带有整数的
ArrayList
。
Please don't give me complicated code since I'm new to Android Studio. 由于我是Android Studio的新手,请不要给我复杂的代码。 If you have a solution to offer, please make as simple as possible.
如果您有解决方案,请尽量简化。
Summary of what I need help with: 我需要帮助的摘要:
-Writes a description on a second activity depending on which item is clicked from a listview. -根据从列表视图中单击的项目,写第二项活动的描述。 Ex: If I click on a recipe for chicken, it will show the recipe for chicken(I have these descriptions as strings, so you don't need to worry about that.)
例如:如果我单击鸡肉食谱,它将显示鸡肉食谱(我将这些描述作为字符串,因此您不必担心。)
-Should I use the listener or a separate method that is called when the item is clicked from the listview(uses android:onClick="whatevermethodnamehere"
in xml instead of how it is now in the main activity with a listener). -我应该使用侦听器还是从列表视图中单击项目时调用的单独方法(在xml中使用
android:onClick="whatevermethodnamehere"
而不是现在在带有侦听器的主要活动中使用的方法)。 Listener, for some reason, does not switch to the next activity and crashes, but the onClick
does not crash and switches, but no description is shown. 侦听器由于某种原因不会切换到下一个活动并崩溃,但是
onClick
不会崩溃并切换,但是没有显示描述。
Why not have the writeDesc method return a STRING of the description instead of an integer. 为什么不让writeDesc方法返回描述的STRING而不是整数。 Looks to me like you are just returning int values...
在我看来,您只是在返回int值...
Since you have already figured out the difficult part ie using ItemClickListener
, I believe you just need some direction, so here it is. 既然您已经找到了困难的部分,即使用
ItemClickListener
,我相信您只需要一些指导,所以就在这里。
Based upon position, put different text in your intent.putExtra
根据位置,在
intent.putExtra
放入不同的文本
eg instead of i.putExtra("recipe_desc", position);
例如,而不是
i.putExtra("recipe_desc", position);
use this i.putExtra("key","recipe_desc");
使用此
i.putExtra("key","recipe_desc");
Now it is upto you how you wish to do it. 现在由您决定如何执行此操作。 I have 2 flavors as quick suggestions Flavor1: Make a mapper class that maps
recipe_desc
with proper descriptions. 作为快速建议,我有2种口味。Flavor1:创建一个映射器类,该映射
recipe_desc
以正确的描述映射recipe_desc
。 eg if you receive "chicken" in your recipe_desc
, then this mapper class contain chicken receipe mapped to it. 例如,如果您在
recipe_desc
收到“鸡肉”,则此映射器类包含映射到它的鸡肉recipe_desc
。 In activitB
, simply use switch-case
for get the correct value 在
activitB
,只需使用switch-case
获得正确的值
Flavor2: instead of using mapper class in activityB, directly put the description into your intent.extra
based upon position. Flavor2:不是在activityB中使用mapper类,
intent.extra
根据位置将描述直接放入您的intent.extra
。 Now just display whatever you get from intent. 现在只显示您从意图中得到的一切。 So in flavor2 your
switch-case
moves to activityA
因此,在flavor2中,您的
switch-case
转移到了activityA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.