如何通过搜索获得与listview中的项目相对应的产品ID。 [英] How can I get product id corrosponding with the item in listview with search.

查看:56
本文介绍了如何通过搜索获得与listview中的项目相对应的产品ID。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	// List view
	private ListView lv;
	
	// Listview Adapter
	ArrayAdapter<String> adapter;
	
	// Search EditText
	EditText inputSearch;
	
	
	// ArrayList for Listview
	ArrayList<String> productList = new ArrayList<String>();
	ArrayList<String> ProdIds = new ArrayList<String>();
	public static String selectedProdId;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Listview Data
        productList.add("Item1");
        productList.add("Item2");
        productList.add("Item3");
        productList.add("Item4");
        productList.add("Item5");
        
        // ListView Id that referes to items for ex:  Item1 having Prodid=1,item5>Prodid=5 and so on..
        ProdIds.add("1");
        ProdIds.add("2");
        ProdIds.add("3");
        ProdIds.add("4");
        ProdIds.add("5");
             
        
        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, productList);
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				selectedProdId= ProdIds.get(arg2);
			    Toast.makeText(MainActivity.this, selectedProdId ,Toast.LENGTH_SHORT).show();
	
			}
		});
        
        
        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
				// When user changed the Text
				MainActivity.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							
			}
		});
    }
    
}





我尝试过:



此代码在首次加载时效果很好,并为所有项目提供id。但是当我搜索Item5时,item5中列出了第5项。所以当我点击item5它返回id = 1



我该怎么办?请建议我一些解决方案..在Adv中的数据;)



What I have tried:

this code works well on first load and gives the id for all item. but when i search for "Item5", the item5 listed first in listview. so when i click on item5 it returns id = 1

What Should i have to do?? please Suggest me some solution.. Thnks In Adv ;)

推荐答案

今天早些时候提出了这个问题。我会给你同样的回答:



你错了。您将ListView视为应用程序数据的容器。不要陷入这个非常糟糕的习惯。



UI控件用于显示应用程序所持有的数据模型的直观表示。它们永远不应该用作数据模型。



不要检查ListView的数据。检查您所持有的数据,而不是您要查找的项目。



通过执行此操作,您可以将数据模型与UI分开,从而可以使用更多您的代码独立于您正在编写的应用程序类型,例如Windows窗体,WPF,ASP.NET,Mobile,...
This question was asked earlier today. I'll give you the same answer I gave then:

You're going about this wrong. You're treating the ListView as a container of your application data. DON'T get into this very bad habit.

UI controls are there to show a visual representation of the data model your application holds. They should never be used AS the data model.

Don't check a ListView for data. Check the data you hold instead for the item you're looking for.

By doing this, you separate your data model from the UI making it possible to use more of your code independent from the type of application you're writing such as Windows Forms, WPF, ASP.NET, Mobile, ...


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView                 long arg3) {
          // TODO Auto-generated method stub
          //selectedProdId= ProdIds.get(arg2);
          String value = (String) lv.getItemAtPosition(arg2);

          for(int i =0;i<productlist.size();i++)>
          {

              if(productList.get(i).equals(value))
              {
                  prodname= productList.get(i);
                  pId= ProdIds.get(i);

              }

          }

          Toast.makeText(MainActivity.this, prodname+" - "+pId ,Toast.LENGTH_SHORT).show();

      }
  });










This Is the sol. Thanks everyone :)


这篇关于如何通过搜索获得与listview中的项目相对应的产品ID。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆