简体   繁体   中英

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. 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 . The variables are ints because I'm using an ArrayList with Integers.

Please don't give me complicated code since I'm new to 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). 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.

Why not have the writeDesc method return a STRING of the description instead of an integer. Looks to me like you are just returning int values...

Since you have already figured out the difficult part ie using ItemClickListener , I believe you just need some direction, so here it is.

Based upon position, put different text in your intent.putExtra

eg instead of i.putExtra("recipe_desc", position); use this 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. eg if you receive "chicken" in your recipe_desc , then this mapper class contain chicken receipe mapped to it. In activitB , simply use switch-case for get the correct value

Flavor2: instead of using mapper class in activityB, directly put the description into your intent.extra based upon position. Now just display whatever you get from intent. So in flavor2 your switch-case moves to activityA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM