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;
}
}
Last edited by KenDiriwan; January 26th, 2012 at 06:16 AM.
Reason: more detail
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.
The Following User Says Thank You to miXer For This Useful Post:
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.