AutoComplete fixed selection? - java

How do i have an AutoComplete selection based on alphabetic instead of fixed switch cases? The situation is, everything working except when i input keyword with "B" showing Badrul as first suggestion but when clicked it will still refer to the first switch cases which is opening up Adidas.class instead of Badrul.class
Please help, i am new in this. Is AutoComplete suitable for my requirement?
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, Affin.class));
break;
case 2:
startActivity(new Intent(Search.this, AlamArt.class));
break;
case 3:
startActivity(new Intent(Search.this, Badrul.class));
break;
}
}
});
}
static final String[] shops = new String[]
{
"Adidas", "Affin Bank", "Alam Art Gallery", "Badrul"
};
}

You are adding OnItemClickListener to the AutoCompleteTextView. I think you don't know this thing about Item Click Listener.
The position variable in onItemClick() method stores the position of item clicked in the list shown.
You are telling the list shown Badrul as first suggestion, so the position of item you clicked is 0, so that its going to case 0:, so that its calling Adidas.class.
I hope you understood the problem from my answer.

This is obvious behavior as you are referring to the position of the suggestion at current time,so it would be differed according to what user types in. For achieving what you want,You need to change your code according to this:
public class Search extends Activity
{
public void onCreate(Bundle savedInstanceSate)
{
super.onCreate(savedInstanceSate);
setContentView(R.layout.searchshop);
final 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) {
int index=999;
for(int i=0;i<shops.length;i++)
{
if(autoComplete.getText().toString().trim().equals(shops[i]))
{
index=i;
break;
}
}
switch(index)
{
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, Badrul.class));
break;
default:
Toast.makeText(Search.this, "Invalid Selection", Toast.LENGTH_SHORT).show();
}
}
});
}
static final String[] shops = new String[]
{
"Adidas", "Affin Bank", "Alam Art Gallery", "Badrul"
};
}

Mr. Kenneth The solution is already there, Hiral's answer, but you have to modify it, to make error free.
Just do below changes.
Modify the below line of code into 2 parts.
AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
1. Add below line of code before onCreate()
AutoCompleteTextView autoComplete;
2.Modify the original line as below
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

Related

How to know which view was clicked in java?

Let's say that I have two ImageViews, and I want to know which one was clicked. How do I use getView() in this case? It's should be something like that
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
checkViewId();
switch(id){
case 1:
log.d -> ("id = 1");
}
break;
case 2:
log.d -> ("id = 2");
break;
return true;
}
How should I create this checkViewId method?
Make your activity/fragment implement the OnClickListener interface and add the onClick callback:
public class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onClick(View v) {
}
}
When you’re binding the views, set the listener for all of them:
ImageView one = (ImageView) findViewById(R.id.one);
one.setOnClickListener(this);
ImageView two = (ImageView) findViewById(R.id.two);
two.setOnClickListener(this);
Inside the onClick method create your switch:
switch (v.getId()) {
case R.id.one:
// add your code
break;
case R.id.two:
// add your code
break;
default:
break;
}

NavigationDrawer opening new activity

I implemented a basic navigationDrawer, with an array of strings. I want the user to click on something in the list and go to the corresponding Activity.
However, I am completely at loss here. I have read quite a few answers out here, but it still is not clear how to do this.
I have found a useful way for all the items combining the answers. However it is still returning my MainActivity instead of other activities.
The addDrawerItems() function:
public void addDrawerItems() {
mDrawerList = (ListView) findViewById(R.id.navList);
String[] osArray = {"How-to", "Milestones", "Bridge & Beams", "Settings"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: //First item
Intent intent1 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent1);
break;
case 1: //Second item
Intent intent2 = new Intent(MainActivity.this, Bridge_BeamsActivity.class);
startActivity(intent2);
break;
case 2: //Third item
Intent intent3 = new Intent(MainActivity.this, MileStonesActivity.class);
startActivity(intent3);
break;
case 3: //Fourth item
Intent intent4 = new Intent(MainActivity.this, HowToActivity.class);
startActivity(intent4);
break;
default:
}
}
});
}
Now my onCreate where I call the addDrawerItems():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Check if there is any saved data
checkFirstRun();
//Apply saved data
savedData();
//Apply drawer
addDrawerItems();
}
Any help would be much appreciated!
If there are any unclear pieces of code or my explanation, please don't hesitate to reach out.
In case you want to see what is already there, please check out my beta app in the Play Store: https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
Add the following code after setting the adapter:
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos =position;
Intent intent = new Intent(MainActivity.this, CorrespondingActivity.class);
startActivity(intent);
switch (position) {
default:
}
}
});
The intent function will take to the desired activity.
EDIT
v.getId() will get you the id of the view, that is the id registered in the R class. As your array only contains strings, the only way to bind the string with the view Id is to manually check it. Build the listener and log your id to identify it
mDrawerList.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.d("your App","id clicked "+v.getId());
//this view is the item clicked by the user
Intent i = new Intent(getApplicationContext(),DestinyActivity.class)
i.putExtra("optionSelected",v.getId());
startActivity(i);
}
});
you will get an id like 234252341, and for every item of your list a different id which you can associate with the string
ORIGINAL
You have to add a click listener to your ListView and put an action on it depending on the view clicked. Something like this
mDrawerList.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//this view is the item clicked by the user
Intent i = new Intent(getApplicationContext(),DestinyActivity.class)
i.putExtra("optionSelected",v.getId())
startActivity(i)
}
})
In DestinyActivity you have to get extras with Intent.getExtras and do whatever you need.
PD: Code is handwritten, check it with your IDE

Searchview filtering gridview, and get switch case to the result instead of the first switch case

I have implemented a searchview to filter my gridview However I have also set up a switch case, which when you click the the different objects, it opens up a new activity. Example of what happens;
This is the home screen, When you click on the first picture you get the corresponding activity
However, when you type something into the search bar it filters the results. I want to be able to click this picture and go to the proper corresponding activity. This is not the case and instead it goes to the first 'switch case' activity;
Filtered results, Instead of showing the 3rd activity, its shows the first one
So yeah, I understand why it is doing this, so I just need someone to put me down a path which will give me a solution.
Here is my code which handles the switch case of my gridview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.gridView);
gv = (GridView) findViewById(R.id.gridView);
sv = (SearchView) findViewById(R.id.searchView);
final Adapter adapter = new Adapter(this, this.getChampions());
gv.setAdapter(adapter);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return false;
}
});
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(MainActivity.this, Aatrox.class);
startActivity(newActivity);
break;
case 1: Intent newActivity2 = new Intent(MainActivity.this, Ahri.class);
startActivity(newActivity2);
break;
case 2: Intent newActivity3 = new Intent(MainActivity.this, Akali.class);
startActivity(newActivity3);
break;
}
}
});
}
private ArrayList<Champions> getChampions() {
ArrayList<Champions> champions = new ArrayList<Champions>();
Champions p;
for (int i = 0; i < Champions.length; i++) {
p = new Champions(Champions[i], Champimgs[i]);
champions.add(p);
}
return champions;
}
}
I have posted this trying to clarify my last post, which was flagged as a duplicate, to a question asking a completely different question, Thank you.
Remove your switch and Use Below Code
if(ItemClicked.getName.equalIgnorecase("Aatrox")){
Intent newActivity = new Intent(MainActivity.this, Aatrox.class);
startActivity(newActivity);
}elseif(ItemClicked.getName.equalIgnorecase("Ahri")){
Intent newActivity2 = new Intent(MainActivity.this, Ahri.class);
startActivity(newActivity2);
}else if(ItemClicked.getName.equalIgnorecase("Akali")){
Intent newActivity3 = new Intent(MainActivity.this, Akali.class);
startActivity(newActivity3);
}
Note: Keep rest code as it is

Navigation Drawer not working when startActivity is used in first case of selectItem

I have this problem with Google's Navigation Drawer where starting the activity specified in the first case (case 0) in my selectItem method breaks and returns to the previous activity.
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
switch(position) {
case 0:
// Placing any startActivity here will load the activity
// but immediately return to the calling activity.
parent.startActivity(new Intent(parent, Dashboard.class));
break;
case 1:
parent.startActivity(new Intent(parent, Card.class));
break;
}
}
But if I put mDrawerLayout.closeDrawer(mDrawerList); or any other code, it'll work normally.
There are no errors reported when the called activity is closed and no exception is thrown. Any thoughts?
I tried reproducing this and it will not resolve parent. Do you have it declared somewhere else ?
What class are you using this in both Activities and Fragments can use startActivity() without the need for parent.startActivity()
Can you post the complete class?
This works for ok for me.
private void selectItem(int position) {
switch (position) {
case 0:
// goto home screen
Log.d(TAG, "Showing Home");
startActivity(new Intent(this, SettingsActivity.class));
break;
case 1:
// Show Editor
Log.d(TAG, "Showing Editor");
break;
default:
break;
}
}

ListView with onItemClick position

I got an App with ListView and on any row you click it's popup something different.(With PauseDialog). And the headers in any row is in a "array.xml" and the text that will popup in the PauseDialog is in the java code.
the problem is that i need to add a lot of rows to the ListView and i want to know if there is way more efficacious to do it than what i did.
Example of the array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="Row_List">
<item>row1</item>
<item>row2</item>
<item>row3</item>
<item>row4</item>
</array>
</resources>
(just a lot more rows...)
the java code:
public class MainActivity extends Activity implements OnItemClickListener {
ListView list;
ArrayAdapter<String> adaptr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
adaptr = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1
, getResources().getStringArray(R.array.Row_List));
View header = getLayoutInflater().inflate(R.layout.header, null);
list.addHeaderView(header);
list.setAdapter(adaptr);
list.setOnItemClickListener(this);
list.setFastScrollEnabled(true);
}
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Dialog dialog1 = new Dialog(this, R.style.PauseDialog);
dialog1.setContentView(R.layout.dialog1);
TextView text = (TextView) dialog1.findViewById(R.id.tv1);
dialog1.setTitle(R.string.Title);
dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(0));
switch(position) {
case 0:
String url = "My-Site.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
break;
case 1:
text.setText("Description of Row1");
dialog1.show();
break;
case 2:
text.setText("Description of Row2");
dialog1.show();
break;
case 3:
text.setText("Description of Row3");
dialog1.show();
break;
case 4:
text.setText("Description of Row4");
dialog1.show();
break;
}
}
}
So there is a better way to put the description into the dialog?(and i would like if there is a way the all the descriptions will be in a xml file and not in the code)
Thanks!
You can store your descriptions as a string-array in arrays.xml:
<string-array name="desciptions">
<item>item 0 descript</item>
<item>item 1 descript</item>
<item>item 2 descript</item>
</string-array>
.. define an array MainActivity:
static String[] desciptions = null;
.. initialize it in onCreate():
if (null==desciptions) desciptions
= getResources().getStringArray(R.array.descriptions);
.. and replace your switch statement with something like this, accounting for your '0' special case:
if (0==position) {
String url = "My-Site.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} else {
text.setText(descriptions[position]);
dialog.show();
}
Just keep in mind you need to make allowances for your special 0 case, to keep your indexes aligned, such as using text.setText(descriptions[position-1]); or having an empty item 0 description.

Categories