Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old January 25th, 2012, 10:36 PM   #1 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 4
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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"
                
            };
}

KenDiriwan is offline  
Reply With Quote
Sponsors
Old January 26th, 2012, 01:58 AM   #2 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

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.
__________________
Android: Personal Storage, Lars Monsen Facts(Norwegian)
miXer is offline  
Reply With Quote
Old January 26th, 2012, 05:07 AM   #3 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 4
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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;
		            }
		        }
KenDiriwan is offline  
Last edited by KenDiriwan; January 26th, 2012 at 06:16 AM. Reason: more detail
Reply With Quote
Old January 26th, 2012, 02:07 PM   #4 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

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.
miXer is offline  
Reply With Quote
The Following User Says Thank You to miXer For This Useful Post:
KenDiriwan (January 27th, 2012)
Old January 27th, 2012, 01:24 AM   #5 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 4
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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"   }
KenDiriwan is offline  
Reply With Quote
Old January 27th, 2012, 05:31 AM   #6 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

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.
miXer is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:24 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo