I have a listView with 2 items in the list, the first item in position 0 responds to clicks but the other does not! i have tried to copy the same onclick method and changed the if statement to the list item in position 1 but it is not working out for me so i am looking how to get this working if someone could help me? i have included the only code i think is needed to resolve this,
public class TopLevelActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
//Create an OnItemClickListener
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this,
DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
//what ive tried to open the foodCategoryActivity list item
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 1) {
Intent intent = new Intent(TopLevelActivity.this,
FoodCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
}
}
thank you.
Currently using two different click listeners for same ListView to do different on different items click in ListView with is not valid way to do task according to click position in ListView.
Use single click listener and inside onItemClick method use switch-case or if-else ladder for doing task like:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
}
else if (position == 1) {{
}
}
};
and also remove following line which you are using two times:
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
because no need to create listView and call setOnItemClickListener multiple times just do it once.
You can only set one OnItemClickListener
you need to differentiate in the implemented method via either the position or the View object itself:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
//code for drink category
}
else {
//code for food category
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
Related
how to make listView clickable in one list and not clickable in other list if value is null or empty
ListView lv = getListView();
if (TAG_UID.isEmpty() && TAG_UID == null)
{
lv.setEnabled(false);
}
else
{
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Intent in = new Intent(AllProductsActivity.this,
MyActivity2.class);
startActivity(in);
}
});
value get from mysql database, thanks for help
For the position you want make it non-clickable handle it in this manner using return statement
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==2){
return;
}else{
//put your code here
}
}
I'm trying to get an AutoCompleteTextView's ID after I clicked a value on the list. Tried looking up on google and stackoverflow, but the provided answers didn't work. Here's what I've got:
Created the view in my class declaration:
public class ActivityCadastrarCliente extends Activity implements OnClickListener, OnItemClickListener {
AutoCompleteTextView E_Nome_Cliente, E_CPF;
List<String> Nomes = new ArrayList<String>();
...
Associated the view to an XML element:
E_Nome_Cliente = (AutoCompleteTextView)findViewById(R.id.Nome_Cliente);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Nomes.toArray(new String[0]));
E_Nome_Cliente.setAdapter(adapter);
E_Nome_Cliente.setOnItemClickListener(this);
and my onItemClick method is called normally as below:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
//case R.id.Nome_Cliente:
...
//}
}
Does anybody know how I can access this view inside onItemClick? Tried several ways, but I only get exceptions:
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)view.getParent();
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent;
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent.getParent();
I need to identify which view was clicked, because I'm using 3 to 5 AutoCompleteTextView and based on the selected value I'll automatically fill in a bunch of other fields.
Have a look at the class AutoCompleteTextViewClickListener in this answer.
Change your setOnItemClickListener call in the following way:
E_Nome_Cliente.setOnItemClickListener(
new AutoCompleteTextViewClickListener(E_Nome_Cliente, this));
Now you can get the id by accessing the modified view parameter:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view had been modified by AutoCompleteTextViewClickListener
//to contain the original AutoCompleteTextView
switch (view.getId()) {
case R.id.Nome_Cliente:
//...
}
}
An easier way:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adapter adapter = parent.getAdapter();
if (adapter == autoCompleteTextView1.getAdapter()) {
// Do something
} else if (adapter == autoCompleteTextView2.getAdapter()) {
// Do something else
}
}
i am not sure what do you mean by view id? do you want to get the selected value?
if yes, then the below code will do it, otherwise please clarify more what do you need and why you want to access the view itself.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
String selected = adapter.getItem(position);
//}
}
more on adapter methods are here
Use parent.findViewById(R.id.id_of_autocompleteTextView) on the parent of the AutoCompleteTextView.
I have a requirement wherein I want to disable items in the list view.
Say e.g., I have 5 items in the listview out of which I want to only enable 1 item.
Note: disabling means greying out the item.
Following is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1, movies);
I don't want to go with the custom adapter wherein we get the getView().
Is there any other way to implement this functionality?
In the adapter there is a method name isEnabled which you can override. This is called for each row like getview. The onclicklistener will only fire if this function returns true. So try doing that in your custom adapter.
#Override
public boolean isEnabled(int position) {
if(YOUR CONDTITION){
return false;
}
return true;
}
Without adapter:
Then you need to disable the item by getting view at specific position.
Please implement the listener for this method setOnItemSelectedListener . So you can disable any item that you want.
You can also disable item using:
final Set<Integer> disabledPositions = new HashSet<Integer>();
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
ListView listView = new ListView(this);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
if(!disabledPositions.contains(position) {
// do what you want
}
}
});
try this:
final List<Integer> disabledItems = new ArrayList<Integer>();
disabledItems.add(0);
disabledItems.add(2);
lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (disabledItems.contains(arg2)) {
Toast.makeText(getApplicationContext(), "DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(false);
} else {
Toast.makeText(getApplicationContext(), "NOT DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(true);
}
}
});
I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
parent -> The AdapterView where the click happened.
view -> The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position -> The position of the view in the adapter.
id->The row id of the item that was clicked.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.
I have a CHOICE_MODE_SINGLE list with 3 rows. How do I place an onClick event that changes the value of an arbitrary variable, so that if Row 1 is clicked, say, the value of X = 1, and when Row 2 is clicked, the value of X = 2, etc.?
public class List extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] GENRES = new String[] {"Barre", "Buffumville","Hodges"};
}
Its quite simple..rather than extending ListActivity extend it by Activity and just declare listview in your XML file as below:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
initialize listview in your activity:
ListView listView=(ListView) findViewById(R.id.list);
And after setting adapter as you did,set the item click listener of the listview as below in your code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(getBaseContext(), GENRES[position], Toast.LENGTH_SHORT).show();
}
});
By doing this you will get the desired value of that position of listview.
You will surely get the result you want.
You want to set the onItemClickListener on the list view.
listView.setOnItemClickListener(
new OnItemClickListener(
#Override
onItemClick(AdapterView<?> listView, View cell, int position, long id) {
// Code to change variables
}
});
You can implement protected void onListItemClick (ListView l, View v, int position, long id) in your ListActivity, and the position variable lets you know which item was clicked.