• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps AutoCompleteTextView suggestions select take user to the xml?

I want user to just select an item from AutoCompleteTextView and once he selects it,it should bring you to another .xml that i have already created.

Below are my source and the error i marked with //
Code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class Search extends Activity
{
    public void onCreate(Bundle savedInstanceSate)
    {
        super.onCreate(savedInstanceSate);
        setContentView(R.layout.searchshop);
        
        AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
        autoComplete.setAdapter(adapter); 
        autoComplete.setThreshold(1);
        autoComplete.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {

                Intent intent;
                int index=999;
                for(int i=0;i<shops.length;i++)
                {
                    //Cannot refer to a non-final variable autoComplete inside an inner class defined in a different method
                    if(shops[i].equals(autoComplete.getText().toString().trim()))
                    {
                        index=i;
                        break;
                    }
                }
                switch(index)
                {
                    case 0:
                        //The constructor Intent(Search, int) is undefined
                        intent=new Intent(Search.this, R.layout.adidas);
                        startActivity(intent);
                        setContentView(R.layout.adidas);
                        break;
                    case 1:
                        //The constructor Intent(Search, int) is undefined
                        intent=new Intent(Search.this, R.layout.affin);
                        startActivity(intent);  
                        setContentView(R.layout.affin);
                        break; 
                }
            }
        });

        
    }
    static final String[] shops = new String[]
            {
                "Adidas", " Affin Bank", "Alam Art", "Al Amin"
                
            };
}
 
1. You must declare autoComplete as final

2. To start a new Activity, you can not refer to the xml layout. You have to actually create an Activity which uses that layout, and refer to that activity.
 
Ok, now i declared. But i had to change something to avoid local duplicate variable.
Code:
public void onCreate(Bundle savedInstanceSate)
	{
		final int autoComplete;
		super.onCreate(savedInstanceSate);
		setContentView(R.layout.searchshop);
		
		 //I have to change this one from autoComplete to autoCompletee to avoid duplicate variable though
		AutoCompleteTextView autoCompletee = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
		autoCompletee.setAdapter(adapter); 
		autoCompletee.setThreshold(1);
		autoCompletee.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
		{
		    	
		    	
		        Intent intent;
		        int index=999;
		        for(int i=0;i<shops.length;i++)
		        {
		                //the local variable autoComplete may not been initialized
		        	if(shops[i].equals(Integer.toString(autoComplete)))
		            {
		                index=i;
		                break;
		            }
		        }
 
So because i told you to declare autoComplete as final, you thought you had to create another variable of Int declared as final? And rename the actual variable you were asking about? That makes no sense, and your code makes no sense.

Try this:
Code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class Search extends Activity
{
    public void onCreate(Bundle savedInstanceSate)
    {
        super.onCreate(savedInstanceSate);
        setContentView(R.layout.searchshop);
        
        AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
        autoComplete.setAdapter(adapter); 
        autoComplete.setThreshold(1);
        autoComplete.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {

                switch(position)
                {
                    case 0:
                        startActivity(new Intent(Search.this, Adidas.class));
                        break;
                    case 1:
                        startActivity(new Intent(Search.this, AffinBank.class));
                        break; 
                    case 2:
                        startActivity(new Intent(Search.this, AlamArt.class));
                        break; 
                    case 3:
                        startActivity(new Intent(Search.this, AlAmin.class));
                        break; 
                }
            }
        });

        
    }
    static final String[] shops = new String[]
            {
                "Adidas", " Affin Bank", "Alam Art", "Al Amin"
                
            };
}

I have removed the for loop, because you allready know which item was clicked, its in variabel position. And i have replaced the call to the xml layouts with calls to actual Activitys, which you will have to create.
 
Thanks a million that worked really well. However it seem to reconigze selection based on fixed position.

For example, when i enter keyword with "B" the autoComplete will show Badrul as the first suggestion and when i click it brings me to the first case which is Adidas.class. I hope you understand what i am trying to do, my english is very bad.
Code:
@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
		{
		    	
		    switch(position)
		    {
		    case 0:
		    	startActivity(new Intent(Search.this, Adidas.class));
		    	break;
		    
		    case 1:
		    	startActivity(new Intent(Search.this, Affin.class));
		    	break;
		    case 2:
		    	startActivity(new Intent(Search.this, AlamArt.class));
		    	break;
		    case 3:
		    	startActivity(new Intent(Search.this, AlAmin.class));
		    	break;
                    case 4:
		    	startActivity(new Intent(Search.this, Badrul.class));
		    	break;
                    }
                }




static final String[] shops = new String[]
			{
				"Adidas", "Affin Bank", "Alam Art Gallery", "Al Amin Kids", "Badrul Songket"   }
 
Try to use arg3 in the switch instead.
position variable should reflect which position you have clicked in the list, and since Badrul is the first item this is 0.
arg3 variable should reflect the actual position in the array.
 
Back
Top Bottom