Android delete selected item using contextual actionbar - android

So far, I have managed to follow this tutorial and run it successfully. But what I wanna do now is to actually delete selected items on my listview. This is the code I'm using:
private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"};
private SelectionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new SelectionAdapter(this,
R.layout.row_list_item, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
mAdapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
mode.finish();
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");
}
});
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return false;
}
});
}
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
Question is how do I delete the selected item/s? I can't figure it out and if anyone of you knows this and can help, I'd gladly appreciate it. Thanks.

In your code instead of using string [] data you can use ArrayList<String> data since it is dynamically sized array where the object is actually removed and the list (array) size is adjusted accordingly. With this change you can use following method.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
List<Integer> keyList = new ArrayList<Integer>(noteAdptr.getCurrentCheckedPosition());
Collections.sort(keyList, new Comparator<Integer>() {
#Override
public int compare(Integer lhs, Integer rhs) {
return lhs.compareTo(rhs);
}
});
Collections.reverse(keyList);
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
for (Integer i:keyList){
Log.e("", "items selected is : "+i);
data.remove((int)i);
}
mAdapter .notifyDataSetChanged();
mAdapter.clearSelection();
mode.finish();
}
return true;
}
Hope this helps

You can set a OnLongClickListener on the items in the ListView that just gets the item that is long pressed.
Afterwards you can reference/remove that object in your OnActionItemClicked.

I am not sure if you have already solved your problem, but here is what worked for me:
Create a private (or public) variable in your class: private int selectedPosition;
In your onItemLongClick() method (as far as I see that's the one, where you are selecting an item), put:
selectedPosition = position;
in OnActionItemClicked() you can get your adapter and do this:
MyAdapter adapter = (MyAdapter) this.getAdapter();
adapter.remove(adapter.getItem(selectedPosition);
adapter.notifyDataSetChanged();
That way you passed the position of the selected item to your adapter.
Hope that helps!

You have to code the functionality yourself: in the onActionItemClicked, you have to code the logic that will erase selected items from your ArrayList.
Don't forget to call notifyDatasetChanged() on your adapter after that... ;-)

Related

Listview item's background selected error when scrolling

I have a listview in fragment and my problem is when I select items on listview then it's fine, but when I scroll my listview then item's background is checked for another items, but I don't want that. You can see my image, first I select 3 items (Browser, Calendar, Contact), when I scroll listview then 2 items (Dev tool, Camera) have a changed background, if I continue scroll then listview has more items like that.
Here is my code:
#SuppressLint("NewApi") public class Tab2 extends Fragment{
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
public static ApplicationAdapter listadaptor = null;
public static ListView list;
private ActionMode acMode;
private int counterChecked = 0;
private SparseBooleanArray sp;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2test,container,false);
list = (ListView)v.findViewById(R.id.list_view2);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setItemsCanFocus(false);
packageManager = getActivity().getPackageManager();
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(getActivity().getApplicationContext(),R.layout.snippet_list_row, applist);
list.setAdapter(listadaptor);
sp = list.getCheckedItemPositions();
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here i set item's color and unselected color
view.setBackgroundColor(sp.get(position)? 0x9934B5E4: Color.WHITE);
if(counterChecked<1){
acMode = ((AppCompatActivity) getActivity()).startSupportActionMode(mActionModeCallback);
}
String str="";
int i=0;
for(i=0;i<sp.size();i++)
{
if(sp.valueAt(i)){
str+=sp.keyAt(i)+",";
}
}
if(list.isItemChecked(position)){
Log.d("list1", String.valueOf(position));
list.setItemChecked(position, true);
counterChecked++;
}else{
list.setItemChecked(position, false);
counterChecked--;
}
if(counterChecked<1){
mActionModeCallback.onDestroyActionMode(acMode);
}
}
});
return v;
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : list) {
try {
if(isSystemPackage(info)){
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return applist;
}
private boolean isSystemPackage(ApplicationInfo AInfo) {
return ((AInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.cab_menu, menu);
MainActivity.toolbar.setVisibility(View.GONE);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mode.finish();
MainActivity.toolbar.setVisibility(View.VISIBLE);
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
};
}
You actually trigger OnItemClickListener when scrolling which causes a multiple select for items that you didn't want to include.
A better approach would be using a checkBox inside your listView Item and mark the item as selected when the check box is checked: check out this link on how to Get Selected Item Using Checkbox in Listview.
If you incest on using onClick then you can implement longClickListener on your listView Items which may prevent get item selected when scrolling but my advice to you is to go with checkBoxes.
OnLongClickListener Implementation:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
String str=listView.getItemAtPosition(index).toString();
return true;
}
});

How to delete items on the listview in Fragments?

I want to delete the items from the fragments. When long click on the items than a action bar is changed and a delete button appears. I click on the delete button The item seems to be deleted from the list view. But when I restart the app the item appears again.
public class ReadFragment extends Fragment {
ListView spinner;
TextView empty;
String selected;
List<String> list;
String[] filenames;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_read, container,
false);
spinner = (ListView) rootView.findViewById(R.id.spinner2);
this.spinner.setEmptyView(rootView.findViewById(R.id.emptyElement));
getFilenames();
getactionbar();
return rootView;
}
private void getFilenames() {
// TODO Auto-generated method stub
filenames = getActivity().fileList();
list = new ArrayList<String>();
for (int i = 0; i < filenames.length; i++) {
// Log.d("Filename", filenames[i]);
list.add(filenames[i]);
}
ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
android.R.id.text1, list);
spinner.setAdapter(filenameAdapter);
spinner.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selected = ((TextView) view).getText().toString();
Intent intent = new Intent(getActivity(), ReadData.class);
intent.putExtra("key", selected);
startActivity(intent);
}
});
}
private void getactionbar() {
// TODO Auto-generated method stub
spinner.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
spinner.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.ic_action_discard:
deletefiles();
Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
private void deletefiles() {
// TODO Auto-generated method stub
SparseBooleanArray checkedItemPositions = spinner.getCheckedItemPositions();
int itemCount = spinner.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
filenameAdapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
filenameAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
}
}
Can anyone help me deleting the item forever, I mean once it is deleted it should not appear in the listview again when the app is relaunched.
Try, tries it worked here
final Button btnRemove = (Button) findViewById(R.id.button1);
btnRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
lst.remove(adapter.getItem(2));
adapter.notifyDataSetChanged();
}
});;
I don't know if it is intentionally, but you copy the items from the Activity into another array before passing it to the Adapter. So when you call adapter.remove() it only removes the item from the new one leaving the original List in the Activity untuched (guess you save that one to storage in the Activitys onPause or something).

Android delete selected items listview using contextmenu actionbar

So far this is what I've tried:
private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"};
private SelectionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new SelectionAdapter(this,
R.layout.row_list_item, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
mAdapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");
}
});
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return false;
}
});
}
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
But I need to know how to get the selected items and put it inside OnActionItemClicked like what I've tried here:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
I commented out mAdapter.removeSelection() because I don't know what to put inside.
Any ideas? help is pretty much appreciated. Thanks.
Their are multiple things you could do.. The easiest way i think is as follows:
It doesn't work because the onActionItemClicked expects one item. But you select multiple items.
The easiest way is to create a loop in which you handle the normal procedure the onActionItemClicked expects
ListView mListView = getListView();
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
Long[] longConverts = new Long(mListView.getCheckedItemIds());
private List<Long> itemids = new ArrayList<Long>(Arrays.asList(longConverts));
for(int i=0;i<itemids.size();i++){
mListView.remove(itemids.get(i));
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
I think this should work.. It retrieves the selected items first from the ListView then put them in a array and loops over that array to remove the items one by one..
I hope this works for you

How to implement search functionality?

I am having a grid view and I have to add search functionality in the action bar.So that whatever i type to search,the items should come whatever i searched.Grid view is having image and text.I stuck in this very badly.I dont know how to implement it.I gone through android guide but I unable to get it.
Please suggest some easy way to do it.I need search like in watsaap application.
Any suggestion is appreciated.Thanks.
Here is my gridview code.
public class FragmentDeals extends Fragment implements Checkable{
private boolean mChecked;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragmentdeal,
(ViewGroup)
findViewById(R.id.layoutdeal));
//View view = inflater.inflate(R.layout.griddeal,null);
final GridView mGrid = (GridView) layout.findViewById(R.id.GridDeal);
mGrid.setAdapter(new DealAdapter());
mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
//listView.setLongClickable(false);
int selectCount = mGrid.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount +"items selected");
break;
}
}
});
return layout;
// TODO Auto-generated method stub
}
#Override
public boolean isChecked() {
// TODO Auto-generated method stub
return mChecked;
}
#SuppressWarnings("deprecation")
#Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
mChecked = checked;
layout.setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.bground) : null);
}
#Override
public void toggle() {
// TODO Auto-generated method stub
setChecked(!mChecked);
}
}
private class DealAdapter extends BaseAdapter {
#Override
public int getCount() {
return mThumbIds1.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//CheckableLayout l;
View myView = convertView;
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.griddealitems, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_deal_image);
iv.setImageResource(mThumbIds1[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_deal_text);
tv.setText(names1[position] );
return myView;
}
private Integer[] mThumbIds1= {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
private String[] names1={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
}
I have never tried for search functionality with gridview. But i have done the same for list view. Please have a look on the following link to implement search functionality for listview
Search functionality with list view
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/

Searching a custom listview

I was curious on how to make a listview with a custom adapter searchable. I followed a tutorial and got everything set up, but the app crashes when I try to type anything in the EditText. Now I know why the app crashes, I just don't know how to fix it.
Here is the code to my MainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplication();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.item_ids);
String[] ids = res.getStringArray(R.array.item_names);
TypedArray icons = res.obtainTypedArray(R.array.item_images);
setListAdapter(new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Get the options menu view from menu.xml in menu folder
getSupportMenuInflater().inflate(R.menu.items_menu, menu);
// Locate the EditText in menu.xml
editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();
// Capture Text in EditText
editsearch.addTextChangedListener(textWatcher);
// Show the search menu item in menu.xml
MenuItem menuSearch = menu.findItem(R.id.menu_search);
menuSearch.setOnActionExpandListener(new OnActionExpandListener() {
// Menu Action Collapse
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
return true;
}
// Menu Action Expand
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
// Show the settings menu item in menu.xml
MenuItem menuSettings = menu.findItem(R.id.home);
// Capture menu item clicks
menuSettings.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent2 = new Intent(ItemId.this, Home.class);
startActivity(intent2);
return true;
}
});
return true;
}
// EditText TextWatcher
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
};
And here is the code to my custom listview adapter:
public class ItemIDAdapter extends ArrayAdapter<String> {
public LayoutInflater mInflater;
public String[] mStrings;
public String[] mIds;
public TypedArray mIcons;
public int mViewResourceId;
ArrayAdapter<String> adapter;
public ItemIDAdapter(Context ctx, int viewResourceId,
String[] strings, String[] ids, TypedArray icons) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIds = ids;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
TextView tv1 = (TextView)convertView.findViewById(R.id.itemids);
tv1.setText(mIds[position]);
return convertView;
}
}
The reason the app crashes when I try to search, is because in the TextWatcher, this line: adapter.getFilter().filter(text); adapter is only defined at the top of my main activity, but it isn't used with my listview at ALL. I'm stuck here and really don't know what to replace adapter with, because ItemIDAdapter doesn't work. Thanks for your help!
Replace
setListAdapter(new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons));
with
adapter = new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons)
setListAdapter(adapter );

Categories

Resources