How to start Activity in adapter?

Just pass in the current Context to the Adapter constructor and store it as a field. Then inside the onClick you can use that context to call startActivity(). pseudo-code public class MyAdapter extends Adapter { private Context context; public MyAdapter(Context context) { this.context = context; } public View getView(…){ View v; v.setOnClickListener(new OnClickListener() { void … Read more

What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

I see these methods as a cleaner approach to accessing my list’s data. Instead of directly accessing my adapter object via something like myListData.get(position) i can simply call the adapter like adapter.get(position). The same goes for getItemId. Usually I would use this method when I want to execute some task based on the unique ID … Read more

AlphabetIndexer with Custom Adapter managed by LoaderManager

So I finally got this to work. Here’s how i did it: I added: ListView lv = getListView(); lv.setFastScrollEnabled(true); lv.setScrollingCacheEnabled(true); to the onLoadFinished() method after the new cursor was swapped in like so public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Swap the new cursor in. (The framework will take care of closing the // … Read more

Filtering ListView with custom (object) adapter

You need to do a few things: 1) In your activity, register for a text change listener on your EditText that contains the value the user enters: mSearchValue.addTextChangedListener(searchTextWatcher); 2) Create your searchTextWatcher and have it do something: private TextWatcher searchTextWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) … Read more

Call Activity method from adapter

Yes you can. In the adapter Add a new Field : private Context mContext; In the adapter Constructor add the following code : public AdapterName(……, Context context) { //your code. this.mContext = context; } In the getView(…) of Adapter: Button btn = (Button) convertView.findViewById(yourButtonId); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (mContext … Read more

How to set selected item of Spinner by value, not by position?

Suppose your Spinner is named mSpinner, and it contains as one of its choices: “some value”. To find and compare the position of “some value” in the Spinner use this: String compareValue = “some value”; ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinner.setAdapter(adapter); if (compareValue != null) { int spinnerPosition = adapter.getPosition(compareValue); mSpinner.setSelection(spinnerPosition); }