I have an app that has a SimpleCursorAdapter. I can get the content of the DB table to show up in the list but i'd like to do something when the item in the list is clicked. When i go to source in eclipse and try to override a clickListener there is nothing to override. I'm looking for a method to override like onListitemClick. How would i do ths?
Eclipse is also complain about the method onListItemClick, sayimg that it must override or implement a supertype method. If i delete the #Override annotation then that error goes, the list is displayed but no event is fired from touching an item in the list.
private class MyAdapter extends SimpleCursorAdapter {
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
}
#Override
public
View getView(int position, View convertView, ViewGroup parent) {
Log.e(TAG, "inside myadapter getview");
View v = super.getView(position, convertView, parent);
if(v == null)
return null;
Cursor c = (Cursor)getItem(position);
String phoneName = c.getString(c.getColumnIndex(LoginValidate.C_PHONE_NAME));
String phoneNumber = c.getString(c.getColumnIndex(LoginValidate.C_PHONE_NUMBER));
((TextView)v.findViewById(R.id.phonename)).setText(phoneName );
((TextView)v.findViewById(R.id.phonenumber)).setText(phoneNumber);
((TextView)v.findViewById(R.id.phonename)).setTextColor(Color.BLACK);
((TextView)v.findViewById(R.id.phonenumber)).setTextColor(Color.BLACK);
return v;
}
}// end of adapter
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.e(TAG, "clicked an item in list");
}
The adapter doesn't have an onListItemClick() method by default, which is why the annotation is bothering it.
You have to tie in the onListItemClick() to the listView... try using it by
ListView list;
list.setOnListItemClick(listAdapter); //if your adapter implements OnListItemListener
or just like this
list.setOnListItemClickListener(new OnListItemClickListener()
{
#override
public void onClick(args)
{
listAdapter.onListItemClick(args); //you will want this to be the first or the last call here
//maybe some other stuff if you want here
}
}
and that should do it.
To sum up:
1) either implement the correct interface in your adapter and it will require the override annotation
2) leave the method exposed in the adapter and call it from a private onListItemClickListener attached to the listview.
Yes turtleboy,
you can do this by overriding a method in you Activity where the listview is placed.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
//do what you want to do
}
});
Related
I have a fragment which extends ListFragment.
In it I use an ArrayAdapter
public class MyFragment extends ListFragment {
public void onStart() {
super.onStart();
List<String> list = new ArrayList<String>();
list.add("Superman");
list.add("Batman");
arrayAdapter = new ArrayAdapter<OptionToStringAdapter>(
getActivity(),
android.R.layout.simple_list_item_1,
list);
getListView().setAdapter(arrayAdapter);
getListView().setOnItemClickListener(new ModifyOption(this));
setListShown(true);
}
When the user taps an item I want to modify the text.
How do I modify the text?
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
???What goes here????
}
}
Assuming you want to change the content of the list item you clicked.
First, change the value using a dialog or widget of your choice.
Second, change the value of the item at the position in the list by using 'position' argument.
Third, call notifyDataSetChanged() method of the adapter object.
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView parent, View view, int position, long id) {
// create a dialog or other widget
String newValue = "your_new_value";
list.set(position,newValue);
arrayAdapter.notifyDataSetChanged(); //informs views that data has been changed
}
}
try with below code:
public class ModifyOption implements OnItemClickListener { public void onItemClick(AdapterView parent, View view, int position, long id) {
arrayAdapter.remove(arrayAdapter.getItem(position));
arrayAdapter.insert("new text",position);
} }
This seems to do the trick. I don't know if this is an appropriate way though.
public void onItemClick(AdapterView parent, View view, int position, long id) {
TextView textview = (TextView) view.findViewById(android.R.id.text1);
textview.setText("Spiderman");
}
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 layout for list item, which consists of two LinearLayouts. What I want to achieve is: when item is clicked, second LinearLayout should become visible/gone, depending on the current visibility.
I am experimenting with this code:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.getViewById(R.id.id_of_the_second_linear_layout).setVisibility(View.GONE);
}
});
However when item is clicked, several other linear layouts (in different items) become visible/hidden. Why?
Update:
Adapter:
public class ExpensesCursorAdapter extends SimpleCursorAdapter implements SimpleCursorAdapter.ViewBinder {
public ExpensesCursorAdapter(Context context, Cursor cursor) {
super(context, R.layout.single_expense, cursor,
new String[]{
ExpenseContract._AMOUNT,
CategoryContract._NAME,
ExpenseContract._DATE
},
new int[]{
R.id.expense_amount,
R.id.expense_category,
R.id.expense_date
},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setViewBinder(this);
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = super.getView(position, convertView, viewGroup);
final View expandablePanel = v.findViewById(R.id.expandable_panel);
expandablePanel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
expandablePanel.setVisibility(view.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
});
return v;
}
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(ExpenseContract._AMOUNT)) {
return handleAmountView((TextView) view, cursor);
}
else ...
return false;
}
private boolean handleAmountView(TextView view, Cursor cursor) {
TextView textView = (TextView) view;
Double amount = ExpenseDbHelper.getAmount(cursor);
String formattedAmount = new DecimalFormat("##.00").format(amount);
textView.setText(formattedAmount);
return true;
}
}
Each item has LinearLayout already added in XML, I want to toggle visibility flag, if possible.
You are writing your logic on wrong places. You want to listen clicks of views inside listitem. Write your logic in Adapter's getView method. In your getView logic can be like this
ll1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ll2.setVisibility(View.GONE);
}
});
ll2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ll1.setVisibility(View.GONE);
}
});
Something like this.
When you scroll through the list items, some layouts will hide and un-hide, in that case, if you are targeting just one view to be visible at a time (that is just considering one cell at a time), then you could maintain the position of the item clicked, or the id, since you are using a cursorAdapter. Else if you are considering more than one cell then maintain a list where in you store each id of the cell that has been tapped on.
Pass the list or the single position value to the adpater, and in the getview compare the id or position and then perform the visiblity code.
Hope this hint helps.
I'm just learning how to use ListViews. I got it working, but wont to be able to respond when some one clicks a item.
I'm trying to use the setOnItemClickListener method to take a call back for when a item is clicked on. But my code will not compile due to errors in method setOnItemClickListener
r
Right now i get a error that says
setOnItemClickListener is not applicable for arguments OnItemClickListener();
void SetUpList()
{
listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
EventsAdapter adapter = new EventsAdapter(this, cGlobals.eventsTitle);
// Assign adapter to ListView
listView.setAdapter(adapter);
// this is whare I get the error listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
}
});
}
}
First make sure you have imported this class:
import android.widget.AdapterView.OnItemSelectedListener;
Next you need to call setOnItemClickListener() like so:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override // "#Override" is required for Java 1.6, but forbidden in 1.5
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
});
Or if your activity implements OnItemClickListener: You need to add the onItemClick() method outside your onCreate() method:
#Override
public void onCreate(Bundle savedInstanceState) {
// Do Something
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do something else
}
(Of course, if you are extending a ListActivity or ListFragment you should override onListItemClick() instead of onItemClick() like the second approach.)
friends,
i am using following code to display list with radio buttons
now i want to select specific radio button of list by default so using setSelection property which does not work.
final String [] items=new String[]{"Item1","Item2","Item3","Item4"};
ArrayAdapter<string> ad=new ArrayAdapter<string>(this,android.R.layout.simple_list_item_single_choice,items);
list=(ListView)findViewById(R.id.List);
list.setAdapter(ad);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelection(2);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText(list.getItemAtPosition(arg2).toString());
}
}
);
please guide what mistake am i doing?
You'r looking for:
list.setItemChecked(2, true);
I might be completely off, but I think setSelection doesn't necessarely checks your item (as in checkbox, or radio), it navigates to it though.
As a workaround (maybe there is a more elegant solution) you can extend ArrayAdapter and set checked manually in a getView() method.
Add something like this to your class:
private static class MArrayAdapter extends ArrayAdapter<String> {
public Adapter(final Context context, final String[] objects) {
super(context, android.R.layout.simple_list_item_single_choice, objects);
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final CheckedTextView view = (CheckedTextView) super.getView(position, convertView, parent);
view.setChecked(position == 2);
return view;
}
}
And change your way of getting an adapter to new MArrayAdapter(this, items);
P.S.
On my previous comment, my mistake, you better call setChoiceMode (it's just in my app, I call notifyDataSetChanged, so I don't really need it). I think your'r up to some weird behaviour without choice mode.