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
}
});
}
}