0

So, I have a ListView which displays different kinds of recipes and you can search the different recipes. What I can't seem to figure out is how I can assign an id to each recipe (which is a String). My first approach was to do an if/if else as stated below, and check for the item clicked. If it matched the exact string, then it called the .class which would then display the recipe. But that is not working.

What do I need to do in order to assign the recipes "products" with an ID which i can use in the onClickItem?

All help is appreciated.

Thank you.

// List view
private ListView lv;


// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;


// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.side8);



   // Listview Data
   final String products[] = {"Pasta med kødsovs", "Lakselasagne", "Kyllingelasagne", "Alm. lasagne", "Lakseruller",
           "Kyllinge/avocado salat", "Koldskål m. kammerjunker", "Omelet",
           "Caesar salat", "Tortilla wraps med kylling", "Frikadeller med kartofler"};


   lv = (ListView) findViewById(R.id.list_view);
   inputSearch = (EditText) findViewById(R.id.inputSearch);

   // Adding items to listview
   adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
   lv.setAdapter(adapter);

   lv.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
           if (products.equals("Pasta med kødsovs")) {
               Intent intent = new Intent(Opskrifter.this, OpskriftPasta.class);
               startActivity(intent);      
               finish();  
            } else if (products.equals("Lakseruller")) {
                Intent intent = new Intent(Opskrifter.this, OpskriftLaks.class);
                startActivity(intent);      
                finish(); 
            } else if (products.equals("Omelet")) {
                Intent intent = new Intent(Opskrifter.this, OpskriftOmelet.class);
                startActivity(intent);      
                finish(); 
            }

            }

        }
   );

inputSearch.addTextChangedListener(new TextWatcher() {

       @Override
       public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
           // When user changed the Text
           Opskrifter.this.adapter.getFilter().filter(cs);  
       }

       @Override
       public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {
           // TODO Auto-generated method stub

       }

       @Override
       public void afterTextChanged(Editable arg0) {
           // TODO Auto-generated method stub                         
           }
       });

   }

}
1
  • You don't need id to refer to item of simple listview. Use position in onItemClick for that. Commented May 1, 2014 at 14:39

3 Answers 3

0

u can create Custom Adapter by extending BaseAdapter there u can create List of Strings when selecting something from the List use the Adapter function getItem which return ur desired String

the Function getItem Accpets int position

getItem(int position)

just use the Postion u get from the Function onClick

Sign up to request clarification or add additional context in comments.

3 Comments

Or you can go further and create recipe object that will have its own field that will be ID
But when you filter the strings by the search field the position changes. Wont this only work if the position remains the same all the time? In order to give the corresponding position its activity
Check out the next link can help u :stackoverflow.com/questions/10532194/…
0

Use the position value in the adapter to create a switch case.

lv.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    switch (position) {
    case 0:
    //Pasta med kødsovs  
    Intent intent = new Intent(Opskrifter.this, OpskriftPasta.class);
           startActivity(intent);      
    finish();
    break;

    case 1:
    //Lakseruller  
    Intent intent = new Intent(Opskrifter.this, OpskriftLaks.class);
           startActivity(intent);      
    finish();
    break;
    //Add more cases here similarly. 
    }

    });

}

For a search functionality, implement a custom adapter.

You can create a Product Class with the following fields,

Class Product{
    int id;
    String name;

    Product(int id, String name){
        this.id = id;
        this.name = name;
    }

    public int getID(){
    return id;
   }
}

ArrayList<Product> products = new Arraylist<Product>();
//Add Products to your arraylist
products.add(new Product(0, "OpskriftPasta")); 
products.add(new Product(1, "OpskriftLaks")); 
//Add more items similarly. 

// In the item click listener of custom adapter, 
// use the position to the get the selected Product Object
//OnItemClick
switch(products.get(position).getID())
{
   //Write switch case here. 
}

2 Comments

Your code works perfect for the intention of a static listview, but the way this has to work is with a search function. So the position (case 0,1,2..) will change if you choose to search for a specific item. I.e. you search for "Lakseruller" which is case 1, but then it becomes the only item in the listview = it is not case 0 -> you get the incorrect page.
Edited for a custom adapter.
0

I've figured out a simpler way to do it:

Create a HashMap like:

final HashMap<String, Class<?>> hm = new HashMap<String, Class<?>>();

then put the products according to the order in the string with corresponding .class activity:

hm.put(products[0]), TheClass.class);
hm.put(products[1]... etc etc for as many products there is.

It's not the 'correct' way but it was much more simple that way.

Lastly put this in the intent with

hm.get(adapter.getItem(posititon)); 

after the .class we're in and start the activity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.