I have a ListView with a custom Adapter that extends ArrayAdapter. It's a ArrayAdapter of Type Artist.
Artist is a very small class that has a name and an id. The Artist Class has toString() overridden to return just the name.
I have an EditText. The EditText has an TextChangeListener where i call .getFilter().filter(chars, callback) on my adapter.
In the Filter.Filterlistener().onComplete() callback i print the count and it looks really good. As i type the count decreases. So it seams everything works as advertised, but the List stays the same. I tried to call artistAdapter.notifyDataSetChanged() to force the list to redraw, but nothing happens. [see 2.)]
I am tinkering around for days now! I am desperate.. Hopefully someone can have a look on my code and tell me what i am doing wrong!
Thanks!
Here is what i have done:
1.) Defined a ListView and an EditText like this:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_search_text"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:layout_below="#id/header">
</EditText>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
2.) Setup my ListView in the Activities onCreate():
private ListView listView = null;
private ArtistAdapter artistAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_artists);
artistAdapter = new ArtistAdapter(this, R.layout.row, list); // 'list' is an ArrayList<Artist>
listView = (ListView) findViewById(R.id.list_search);
listView.setAdapter(artistAdapter);
listView.setFastScrollEnabled(true);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// do something
}
});
EditText txtSearch = (EditText) findViewById(R.id.list_search_text);
txtSearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) { }
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
public void onTextChanged(CharSequence chars, int start, int before, int count) {
artistAdapter.getFilter().filter(chars, new Filter.FilterListener() {
public void onFilterComplete(int count) {
Log.d(Config.LOG_TAG, "filter complete! count: " + count);
artistAdapter.notifyDataSetChanged();
}
});
}
});
}
3.) This is my ArtistAdapter in short. I added an remove() and add() method:
public class ArtistAdapter extends ArrayAdapter<Artist> implements SectionIndexer {
private List<Artist> items;
/* other stuff like overridden getView, getPositionForSection, getSectionForPosition and so on */
#Override
public void remove(Artist object) {
super.remove(object);
items.remove(object);
}
#Override
public void add(Artist object) {
super.add(object);
items.add(object);
}
}
4.) My artist has also the toString() overridden:
public class Artist implements Comparable<Artist> {
public String uid;
public String name;
public Artist(String id, String name) {
this.uid = id;
this.name = name;
}
public int compareTo(Artist another) {
return this.name.compareToIgnoreCase(another.name);
}
#Override
public String toString() {
return this.name;
}
}
I found the solution. I have rewritten the whole code from scratch and found the problem.
In my custom ArrayAdapter called ArtistAdapter (see 3. in question) if have a var to store the items:
private List items;
And in my overridden getView() when i want to get the current item i did:
items.getItem(position)
now i do a:
getItems(position) (so the item is retrieved from the storage of the base class and not my own) and this seems to do the trick!
This is my getView() as it is now (the version, that is working):
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout artistView;
Artist art = getItem(position);
if (convertView == null) {
artistView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resId, artistView, true);
} else {
artistView = (LinearLayout) convertView;
}
TextView nameView = (TextView)artistView.findViewById(R.id.txt_name);
TextView uidView = (TextView)artistView.findViewById(R.id.txt_uid);
nameView.setText(art.name);
uidView.setText(art.uid);
return artistView;
}
Pfu, this was a really annoying bug...
Try using listView.setFilterText() rather then adapter filter.
You have to override the toString() method, to return the text you want to filter.
Related
AutoCompleteTextView lets users choose a string from a list of valid values. Like, I imagine, every developer who wants to use this yoke, I am much more interested in the id of the user's selection than its string label. Is there any way to retrieve the id property of a chosen object, or its index in the source array?
The following C# code let's users pick from a list of SomeObject. I'm working in Xamarin, but don't let this put you off. Fix my problem in java and I'll happily make it work in C#
public class AutoCompleteField : PhysicalField
{
protected AutoCompleteTextView actv;
public AutoCompleteField(IList<SomeObject> choices, LogicalField logical, string id)
: base(logical, id)
{
_choices = choices;
}
protected ArrayAdapter<SomeObject> _adapter;
public override void addToView(LayoutInflater inflater)
{
var ctx = App_NotMobility.CurrentActivity;
actv = new AutoCompleteTextView(ctx);
actv.Id = _form.generateId();
// test choices
var _choices = new List<SomeObject>();
_choices.Add(new SomeObject(234, "Oranges"));
_choices.Add(new SomeObject(456, "Apples"));
_choices.Add(new SomeObject(789, "Bananas"));
_adapter = new ArrayAdapter<SomeObject>(ctx, Android.Resource.Layout.SimpleDropDownItem1Line, _choices);
actv.Adapter = _adapter;
actv.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e)
{
// HOW DO I ACCESS THE ID OR THE INDEX OF USER'S SELECTION ?????????????????
};
_form.AddView(actv);
}
}
public class SomeObject
{
public int Id { get; set; }
public string Label { get; set; }
public SomeObject(int id, string label)
{
Id = id;
Label = label;
}
public override string ToString()
{
return Label;
}
}
Once you have initialized the adapter and overdid the item click, all you need to do is get the particular object from your adapter at that particular position of item which you clicked.
In java it would be somewhat similar to,
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeObject someObject = (SomeObject) adapter.getItem(position);
int id = someObject.getId();
}
Thats all you would need. I am not sure about your code in xamarin, how you would get the position because i don't see any method where the position is input variable, may be you could add the tag to your view and can get the tag on your click method.
var position = ((View)sender).Tag;
But i would recommend , if you can create a class extend the base adapter, that way you will the method GetView to override and can easily do what you are looking for. You constructor could be like this for start,
List<SomeObjects> items;
Activity context;
public CustomAdapter(Activity context, List<SomeObjects> items)
: base()
{
this.context = context;
this.items = items;
}
Ankush's answer worked. I'm posting the C# code here because there are some subtleties with casting and generics...
public class myActv : AutoCompleteTextView, AdapterView.IOnItemClickListener
{
PhysicalField _physical;
public myActv(Activity ctx, PhysicalField physical) : base(ctx)
{
OnItemClickListener = this;
_physical = physical;
}
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
// This is the punchline...
SomeObject whatIwant = ((ArrayAdapter<SomeObject>)this.Adapter).GetItem(position);
}
}
Here searchText is an Autocompletetextview..
searchText.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
int position = position; //Adapter selection position
}
});
I have two classes that I'm working with. Contacts and CustomAdapter. In my Contacts Class I have an onActivityResult() method, which gets data from a different activity and places it in a Custom ListView using my CustomAdapter Class. The data gets added fine. Each row consists of a name, email, phone number AND a Button Widget. My question is, I would like to be able to press this Button and have that specific row be deleted. I've tried a number of different things but nothing seems to be working.
I placed the code below. If anyone has any suggestions on the best way to do this, I would greatly appreciate it. Thank you.
onActivityResult in Contacts Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE){
if(resultCode == RESULT_OK){
String name = data.getStringExtra("name");
String phone = data.getStringExtra("phone");
final String email = data.getStringExtra("email");
//These are array lists declared earlier
phoneNums.add(phone);
names.add(name);
emails.add(email);
customAdapter = new CustomAdapter(Contacts.this,names,phoneNums,emails);
contactList.setAdapter(customAdapter);
contactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
names.remove(position);
phoneNums.remove(position);
phoneNums.remove(position);
//This method is still not being recognized
contactList.getAdapter().notifyDataSetChanged()
//This one is but the app is crashing when I click on any of the rows
contactList.getAdapter().notify()
}
});
}
}
}
Custom Adapter Entire Class:
public class CustomAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private ArrayList<String>phoneNumbers;
private ArrayList<String>names;
private ArrayList<String>emails;
private static LayoutInflater inflater = null;
public CustomAdapter(Context c,ArrayList<String>n,ArrayList<String>nums,ArrayList<String>e){
context = c;
phoneNumbers = nums;
names = n;
emails = e;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return names.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View view = convertView;
if (view == null){
view = inflater.inflate(R.layout.contacts_custom_row,null);
Button deleteBtn = (Button)view.findViewById(R.id.customRowDeleteButton);
TextView name = (TextView)view.findViewById(R.id.customRowContactName);
TextView phone = (TextView)view.findViewById(R.id.customRowContactNumber);
TextView email = (TextView)view.findViewById(R.id.customRowContactEmail);
name.setText(names.get(position));
phone.setText(phoneNumbers.get(position));
email.setText(emails.get(position));
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//SHOULD I PLACE CODE TO DELETE THE ROW IN HERE?
}
});
}
return view;
}
Basically what you need is one List<>to rule the size of you List (i believe yours is the names. For that, your getItem(int position) has to return names.size(). To delete a specific row, you just need to delete the index of names that you want, and call notifyDataSetChanged() in your adapter after that.
I have a listview and a button in my layout file. I'am adding items to listview on click of that button. The listview should be empty when the activity is started but it should grow by adding the items to it.
This is my code inside onCreate() :
list = (ListView)findViewById(R.id.inverterListView);
adapter = new ArrayAdapter<String>(InverterList.this, R.layout.inverters_list_row, R.id.inverterNumberTextViewInPanelListRow);
list.setAdapter(adapter);
And here iam adding the items to listview onclick of a button.
adapter.add(inverterNo);
adapter.notifyDataSetChanged();
This works fine. Can anyone guide me to delete custom listview item ? Thanks in advance.
If you know the position of the item you can do this:
Object item = adapter.getItem(position);
adapter.remove(item);
adapter.notifyDataSetChanged();
You may write your own adapter extends BaseAdapter and implement all you need methods.
It is example of my adapter:
public class PeopleUserAdapter extends BaseAdapter
{
private List<User> users;
private int viewResourceId;
private Context context;
public PeopleUserAdapter(Context context, int viewResourceId)
{
this.context = context;
this.viewResourceId = viewResourceId;
this.users = new ArrayList<User>();
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
UserItemHolder holder;
if (convertView == null)
{
convertView = LayoutInflater.from(context).inflate(viewResourceId, parent, false);
holder = new UserItemHolder(convertView);
}
else holder = (UserItemHolder) convertView.getTag();
User user = getItem(position);
holder.name.setText("#" + user.getLogin());
return convertView;
}
#Override
public int getCount()
{
return users.size();
}
#Override
public User getItem(int position)
{
return users.get(position);
}
#Override
public long getItemId(int position)
{
return getItem(position).hashCode();
}
public void clear()
{
users.clear();
}
public void addAll(Collection<User> users)
{
this.users.addAll(users);
notifyDataSetChanged();
}
public void replace(Collection<User> users)
{
clear();
addAll(users);
}
public static PeopleUserAdapter init(Context context)
{
return new PeopleUserAdapter(context, R.layout.item_user);
}
}
adapter.remove(item) .. and then call adapter.notifyDataSetChanged();
In case you are using a custom adapter (for a custom layout listview), you will want to do this:
When your Adapter is something like:
public class YourAdapterName extends ArrayAdapter<yourObject>
then the code for deleting the selected ListView Item will be:
ListView yourListView = (ListView) findViewById(R.id.listviewid);
YourAdapterName adapter;
adapter = (YourAdapterName) yourListView.getAdapter();
yourObject theitem = adapter.getItem(position);
adapter.remove(theitem);
adapte.notifyDataSetChanged();
This is assuming you are inside an event that gives you access to the current position inside the listview. like:
public boolean onItemLongClick(AdapterView<?> parent, View strings,int position, long id)
or
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
Otherwise you will need to obtain that position some other way, like storing it (onItemClick or onItemLongClick) in a textView with Visibility.GONE, and retrieve it when clicking the button (this is silly, you can use all kinds of storage options, like global variables, database and such).
Make sure you have overridden the remove method on your custom adapter
For example if this is your add method:
#Override
public void add(String[] object) {
scoreList.add(object);
super.add(object);
}
then your remove method would look something like this:
#Override
public void remove(String[] object) {
scoreList.remove(object);
super.remove(object);
}
call the below two lines::
adapter.remove(inverterNo);
adapter.notifyDataSetChanged();
where inverterNo is your item
It easy; you only to need is: add a method public in your personalize adapter some this:
public void remove(int position) {
itemsMovieModelFiltered.remove(position);
notifyDataSetChanged();
}
Remenber, this method you must add in your personalize adapter.
Then, call this method from other
adapte=new PersonalizeListAdapter(getActivity().getApplicationContext(),
movieModelList);
adapte.remove(position);
When I use edit text to filter the items, the list positions get all messed up and the items no longer call the proper intent. Any help is appreciated
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String mName = filteredValues.get(position).getName().toString();
String mWeb = filteredValues.get(position).getWebsite().toString();
Intent openDetails = new Intent(Test.this, ResourceDetails.class);
Bundle b = new Bundle();
b.putString("name", mName);
b.putString("web", mWeb);
openDetails.putExtras(b);
startActivity(openDetails);
}
});
private TextWatcher filterTextWatcher = new TextWatcher(){
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
public void afterTextChanged(Editable s) {
}
};
flashsearchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Integer temp=flashSearchNameMap.get(adapter.getItem(position));
navigateSearch(temp);
}
});
(adapter.getItem(position) will return you the exact list name and in flashSearchNameMap i have stored names and position at beginning from oncreate before applying filtering.So you can get exact position by this
if you use ViewHolder in Adapter just define a realPosition variable in holder class and set it in YourAdapter.getView
and in listClick Listener
ContactAdapter.ViewHolder holder = (YourAdapter.ViewHolder) view.getTag();
holder.realPosition
The item position is not reliable when using lists. I recommend you to use view.setTag(Object) to assign an identifier to each item when attaching the content. This could be a number, string or anything. Then you can just access it with view.getTag() inside the click listener.
Assuming you are using a custom bean object to store your name & website values and an ArrayAdapter to show them in your ListView, like so
public class NamedLink {
final String mName;
final String mWebsite;
public NamedLink(String name, String website) {
mName = name;
mWebsite = website;
}
#Override
public String toString() {
return mName;
}
}
With an adapter, defined something like this:
mAdapter = new ArrayAdapter<NamedLink>(this, android.R.layout.simple_list_item_2, mLinks) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(WhateverYourActivityIsNamed.this).inflate(android.R.layout.simple_list_item_2, null);
}
NamedLink link = getItem(position);
// This probably deserves a ViewHolder
((TextView) convertView.findViewById(android.R.id.text1)).setText(link.getName());
((TextView) convertView.findViewById(android.R.id.text2)).setText(link.getWebsite());
return convertView;
}
};
When you filter the array adapter it will match against the beans #toString(), which in this case returns the name. When filtered, the array adapter maintains a properly indexed copy of your list of beans internally - i.e. you can use the position you get in the click listener like this:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getItemAtPosition() will return a NamedLink from the filtered
// list maintained inside the ArrayAdapter
NamedLink link = (NamedLink) parent.getItemAtPosition(position);
Intent openDetails = new Intent(Test.this, ResourceDetails.class);
Bundle b = new Bundle();
b.putString("name", link.getName());
b.putString("web", link.getWebsite());
openDetails.putExtras(b);
startActivity(openDetails);
}
});
I've a ListView where every element in the list contains a TextView and two different Buttons. Something like this:
ListView
--------------------
[Text]
[Button 1][Button 2]
--------------------
[Text]
[Button 1][Button 2]
--------------------
... (and so on) ...
With this code I can create an OnItemClickListener for the whole item:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> list, View view, int position, long id) {
Log.i(TAG, "onListItemClick: " + position);
}
}
});
However, I don't want the whole item to be clickable, but only the two buttons of each list element.
So my question is, how do I implement a onClickListener for these two buttons with the following parameters:
int button (which button of the element has been clicked)
int position (which is the element in the list on which the button click happened)
Update: I found a solution as described in my answer below. Now I can click/tap the button via the touch screen. However, I can't manually select it with the trackball. It always selects the whole list item and from there goes directly to the next list item ignoring the buttons, even though I set .setFocusable(true) and setClickable(true) for the buttons in getView().
I also added this code to my custom list adapter:
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
This causes that no list item is selectable at all any more. But it didn't help in making the nested buttons selectable.
Anyone an idea?
The solution to this is actually easier than I thought. You can simply add in your custom adapter's getView() method a setOnClickListener() for the buttons you're using.
Any data associated with the button has to be added with myButton.setTag() in the getView() and can be accessed in the onClickListener via view.getTag()
I posted a detailed solution on my blog as a tutorial.
This is sort of an appendage #znq's answer...
There are many cases where you want to know the row position for a clicked item AND you want to know which view in the row was tapped. This is going to be a lot more important in tablet UIs.
You can do this with the following custom adapter:
private static class CustomCursorAdapter extends CursorAdapter {
protected ListView mListView;
protected static class RowViewHolder {
public TextView mTitle;
public TextView mText;
}
public CustomCursorAdapter(Activity activity) {
super();
mListView = activity.getListView();
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// do what you need to do
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = View.inflate(context, R.layout.row_layout, null);
RowViewHolder holder = new RowViewHolder();
holder.mTitle = (TextView) view.findViewById(R.id.Title);
holder.mText = (TextView) view.findViewById(R.id.Text);
holder.mTitle.setOnClickListener(mOnTitleClickListener);
holder.mText.setOnClickListener(mOnTextClickListener);
view.setTag(holder);
return view;
}
private OnClickListener mOnTitleClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "Title clicked, row %d", position);
}
};
private OnClickListener mOnTextClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "Text clicked, row %d", position);
}
};
}
For future readers:
To select manually the buttons with the trackball use:
myListView.setItemsCanFocus(true);
And to disable the focus on the whole list items:
myListView.setFocusable(false);
myListView.setFocusableInTouchMode(false);
myListView.setClickable(false);
It works fine for me, I can click on buttons with touchscreen and also alows focus an click using keypad
I don't have much experience than above users but I faced this same issue and I Solved this with below Solution
<Button
android:id="#+id/btnRemove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btnEdit"
android:layout_weight="1"
android:background="#drawable/btn"
android:text="#string/remove"
android:onClick="btnRemoveClick"
/>
btnRemoveClick Click event
public void btnRemoveClick(View v)
{
final int position = listviewItem.getPositionForView((View) v.getParent());
listItem.remove(position);
ItemAdapter.notifyDataSetChanged();
}
Probably you've found how to do it, but you can call
ListView.setItemsCanFocus(true)
and now your buttons will catch focus
I am not sure about be the best way, but works fine and all code stays in your ArrayAdapter.
package br.com.fontolan.pessoas.arrayadapter;
import java.util.List;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import br.com.fontolan.pessoas.R;
import br.com.fontolan.pessoas.model.Telefone;
public class TelefoneArrayAdapter extends ArrayAdapter<Telefone> {
private TelefoneArrayAdapter telefoneArrayAdapter = null;
private Context context;
private EditText tipoEditText = null;
private EditText telefoneEditText = null;
private ImageView deleteImageView = null;
public TelefoneArrayAdapter(Context context, List<Telefone> values) {
super(context, R.layout.telefone_form, values);
this.telefoneArrayAdapter = this;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.telefone_form, parent, false);
tipoEditText = (EditText) view.findViewById(R.id.telefone_form_tipo);
telefoneEditText = (EditText) view.findViewById(R.id.telefone_form_telefone);
deleteImageView = (ImageView) view.findViewById(R.id.telefone_form_delete_image);
final int i = position;
final Telefone telefone = this.getItem(position);
tipoEditText.setText(telefone.getTipo());
telefoneEditText.setText(telefone.getTelefone());
TextWatcher tipoTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
telefoneArrayAdapter.getItem(i).setTipo(s.toString());
telefoneArrayAdapter.getItem(i).setIsDirty(true);
}
};
TextWatcher telefoneTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
telefoneArrayAdapter.getItem(i).setTelefone(s.toString());
telefoneArrayAdapter.getItem(i).setIsDirty(true);
}
};
tipoEditText.addTextChangedListener(tipoTextWatcher);
telefoneEditText.addTextChangedListener(telefoneTextWatcher);
deleteImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
telefoneArrayAdapter.remove(telefone);
}
});
return view;
}
}
I Know it's late but this may help, this is an example how I write custom adapter class for different click actions
public class CustomAdapter extends BaseAdapter {
TextView title;
Button button1,button2;
public long getItemId(int position) {
return position;
}
public int getCount() {
return mAlBasicItemsnav.size(); // size of your list array
}
public Object getItem(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.listnavsub_layout, null, false); // use sublayout which you want to inflate in your each list item
}
title = (TextView) convertView.findViewById(R.id.textViewnav); // see you have to find id by using convertView.findViewById
title.setText(mAlBasicItemsnav.get(position));
button1=(Button) convertView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your click action
// if you have different click action at different positions then
if(position==0)
{
//click action of 1st list item on button click
}
if(position==1)
{
//click action of 2st list item on button click
}
});
// similarly for button 2
button2=(Button) convertView.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your click action
});
return convertView;
}
}
Isn't the platform solution for this implementation to use a context menu that shows on a long press?
Is the question author aware of context menus? Stacking up buttons in a listview has performance implications, will clutter your UI and violate the recommended UI design for the platform.
On the flipside; context menus - by nature of not having a passive representation - are not obvious to the end user. Consider documenting the behaviour?
This guide should give you a good start.
http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/