简体   繁体   English

Android ListView onItemClick用于特定页面

[英]Android ListView onItemClick for specific page

So, I have a ListView which displays different kinds of recipes and you can search the different recipes. 因此,我有一个ListView,其中显示了不同种类的食谱,您可以搜索不同的食谱。 What I can't seem to figure out is how I can assign an id to each recipe (which is a String). 我似乎无法弄清楚的是如何为每个配方(一个字符串)分配一个ID。 My first approach was to do an if/if else as stated below, and check for the item clicked. 我的第一种方法是按如下所述进行if / if else,然后检查是否单击了该项。 If it matched the exact string, then it called the .class which would then display the recipe. 如果它与确切的字符串匹配,则它将调用.class,然后将显示配方。 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? 我需要做什么才能为配方“产品”分配一个可以在onClickItem中使用的ID?

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

   }

}

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 您可以通过扩展BaseAdapter来创建自定义适配器,在那里您可以使用Adapter函数getItem从列表中选择内容时创建字符串列表,该函数返回所需的字符串

the Function getItem Accpets int position Function getItem Accpets int位置

getItem(int position)

just use the Postion u get from the Function onClick 只需使用从onClick函数获取的位置

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. 
}

I've figured out a simpler way to do it: 我想出了一种更简单的方法:

Create a HashMap like: 创建一个像这样的HashMap:

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

then put the products according to the order in the string with corresponding .class activity: 然后根据顺序将产品放入具有相应.class活动的字符串中:

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 .class之后,我们进入并开始活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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