Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
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							
			}
		});
    }
    
}


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 ;)
Posted
Updated 18-Apr-16 8:26am
v2
Comments
Richard MacCutchan 16-Apr-16 12:34pm    
Please do not just dump all your code with a vague "it doesn't work", and expect other people to figure out what is happening. Edit your question, show only the failing code and explain exactly where and how it fails.

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, ...
 
Share this answer
 
Comments
FireMonkey018 16-Apr-16 21:43pm    
there must be a solution for it
Dave Kreskowiak 16-Apr-16 22:36pm    
I've already told you what to do. You're just making it harder on yourself and less performant.
FireMonkey018 18-Apr-16 14:25pm    
it wasn't that much hard..
Dave Kreskowiak 18-Apr-16 17:21pm    
I never said it was difficult or easy. All I said was you're getting yourself into a VERY bad habit. Now that you've used a UI control as a data container, you've have tightly bound your code to that control. You can now no longer use this code in any other kind of application, such as a Windows app or a web site or some other app type on Android. It is forever bound to running on one type of Android application you've built. Congratulations.
FireMonkey018 19-Apr-16 11:53am    
I Appreciate your answer. Thank you..!!
Java
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 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900