1

For my application I am using a couple listview. So far have been using simplecursoradapter and works great except for my list view that has a TextView and EditText in the row. I've run into problems that others have had, such as pulling the data from edittext to use in another activity as well as on scroll the text jumps around to different fields due to recycling. After research seems the best way to over come this is to build a custom cursor adapter but I can't wrap my head around how to do this.

public void fillData() {
    Cursor e = mydbhelper.getUserWord();
        startManagingCursor(e);
        String[] from = new String[] {dbadapter.KEY_USERWORD,};
        int[] to = new int[] {R.id.textType,};
       editadapter = new SimpleCursorAdapter(this, R.layout.edit_row, e, from, to);
       ListView list = getListView();
       View footer = getLayoutInflater().inflate(R.layout.footer_layout, list, false);
       list.addFooterView(footer);
       setListAdapter(editadapter);

This is what I use for the activity in question but I can't understand how this would fill into a custom cursor adapter.

Also not sure if this custom adapter should be in a separate class from the rest of my code (like my menu items, edittext validator, onclick events etc.) and call the custom at the start of the class for everything else.

EDIT Was able to figure out what I needed from http://www.vogella.de/articles/AndroidListView/article.html#overview

not sure how I didn't come across it earlier but it helped me build a base to work off of. TY for all the help. I feel like I have ALOT better understand of how custom cursor adapters work.

maebe
  • 553
  • 1
  • 6
  • 18
  • you are putting the itemsAdapter inside the onCreate method. it should be inside the class and not inside onCreate. – AD14 Feb 21 '12 at 08:58

3 Answers3

0

Creating and using Custom Cursor Adapter is very easy, just extend CursorAdapter class into a class, but as in activity you already extended a class, you cant extend CursorAdapter in Activity Class, so you need to create a new class, but you can create a private class in your Activity Class, so it can use fields of your Activity class too.say CustomCursorAdapter. CursorAdapter class has various method init, which you can extend to achieve your purpose. Like, to change views of your adapter, as is your requirements, override newView(), and bindView(), newView is create a new view(inflate from xml, or by creating new objects), and bindView() is to bind some values to existing views. see an example:

public class ExampleCursorAdapter extends CursorAdapter {
    public ExampleCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView summary = (TextView)view.findViewById(R.id.summary);
        summary.setText(cursor.getString(
                cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.item, parent, false);
        bindView(v, context, cursor);
        return v;
    }
}
jeet
  • 29,001
  • 6
  • 52
  • 53
0

For

Also not sure if this custom adapter should be in a separate class from the rest of my       code (like my menu items, edittext validator, onclick events etc.) and call the custom at the start of the class for everything else.

Yes, It should be in a separate class.

In your activity,

ListView lv = (ListView)findViewById(R.id.list);

Cursor c = getFromDb();
CustomCursorAdapter adapter = new CustomCursorAdapter(getApplicationContext() , c);
lv.setAdapter(adapter);

Now create the CustomCursorAdapter

public class CustomCursorAdapter extends CursorAdapter {

public CustomCursorAdapter(Context context , Cursor c) {
    super(context,c);

    // TODO Auto-generated constructor stub
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub

             //Inflate your layout with textview and edittext
             //setText with values from cursor
             //return the view

    return null;
}

}
user936414
  • 7,574
  • 3
  • 30
  • 29
  • Giving this one a try but R.id.list is giving me a head ack. Not resolving so ListView lv = getListView(); did the trick – maebe Feb 21 '12 at 07:15
  • Yes, that's almost perfect, you'd need only to handle in another thread `CursorAdapter adapter = new CursorAdapter...` and `lv.setAdapter(Adapter)`, so that DB Operations take too long. – Gödel77 Jul 03 '14 at 06:07
-2

Try a custom adaptor instead of custom cursor adaptor ...

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnew);

    ItemsAdapter itemsAdapter = 
        new ItemsAdapter(AddNew.this, R.layout.addnew_typelist_row, types);

    listtype.setAdapter(itemsAdapter);
}

public class ItemsAdapter extends BaseAdapter {
    ArrayList<String> items = new ArrayList<String>();
    String type;
    int color;
    ViewHolder holder;
    Context context;
    int resID;

    public ItemsAdapter(Context context, int resID, ArrayList<String> items) {
        this.items = items;
        this.context = context;
        this.resID=resID;
    }

    public View getView(final int position, View convertView, 
        ViewGroup parent) {

        View v = convertView; 
        TextView name = null ;

        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            v = inflater.inflate(resID, null);

            holder=new ViewHolder();
            holder.lt = (RelativeLayout)v.findViewById(R.id.ltlistrow);
            holder.name = (TextView)v.findViewById(R.id.desc);

            v.setTag(holder);

        } else {
            holder = (ViewHolder)v.getTag();
            v = convertView;
        }

        String nam = items.get(position);
        if (nam != null) {
            holder.name.setText(items.get(position));
        }

        return v;
    }

    public static class ViewHolder{
        TextView name;
        RelativeLayout lt ;
    }
}

Custom adapter can be in new class or can be a inner class. inflate a layout with the edit text and set it as the list item in the getView. hope it helps

AD14
  • 1,218
  • 19
  • 32
  • Few things to help me understands this. this.context = context; this.resID=resID; Do I change these to what I need on the right half or are they how they should be? ItemsAdapter itemsAdapter = new ItemsAdapter(AddNew.this, R.layout.addnew_typelist_row, types); is this something like a hashamp or arraylist? I need to pull my data from a database table. Is that what the above is for or do I need bindview, newview for that as I've seen in other examples? – maebe Feb 21 '12 at 05:22
  • ItemsAdapter(AddNew.this, R.layout.addnew_typelist_row, types); Addnew.this is my activity class that is my current context, R.layout.addnew_typelist_row is a xml layout in which iam having the list row layout say i have a edit text and a image for each row. i have that row in this xml. types is the arraylist of some values you need to show it in the list. yes you need to pull it from db or something and put in this list and pass it to the adapter to display it. viewholder is class to hold the items. this way you can set tag and retrive any item like editbox in a listitem. hope it helps – AD14 Feb 21 '12 at 05:38
  • I'm having trouble with a few parts to it. Addnew, not sure what that is suppose to be. The name of my current activity? types, says its can't resolve the variable but not sure what to change to or set the variable to. ItemsAdapter, can't resolve to type. Should the end result be similar to ArrayList editTextList = new ArrayList();? For the holder should it reflect what I call my columns in my database? class ViewHolder{ TextView userword; //Column in db is called userword and that has what needs to be setText of the TextView } – maebe Feb 21 '12 at 05:44
  • For ItemsAdapter if I add type parameter to "myclassname" it resolves the error Im getting but not sure that's the right way of correcting it. – maebe Feb 21 '12 at 05:50
  • public class ItemsAdapter extends BaseAdapter doesn't like that it's public, "Illegal modifier for the local class ItemsAdapter; only abstract or final is permitted" which if I do only causes more problems. – maebe Feb 21 '12 at 06:10
  • Addnew is my activity name. thats the current context. types is the array list. exactly what do you want and if you can post your code i can check and let you know. this is example for a question similat to this http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons. instead of button you can use editbox. – AD14 Feb 21 '12 at 06:16
  • Updated question with what I have. I'll go ahead and take a look are the link – maebe Feb 21 '12 at 06:25
  • Lol this is not cursoradapter – Selvin Feb 21 '12 at 23:28
  • @Selvin i read his question and gave a solution for his problem - "I've run into problems that others have had, such as pulling the data from edittext to use in another activity as well as on scroll the text jumps around to different fields due to recycling" he said he had read custom cursor adapter is the best solution. but i thought this may help him. i have mentioned this is a custom adapter not custom cursor adapter. – AD14 Feb 22 '12 at 04:44