0

after quite a googling without a luck I come here again to bother you all :)

I have ListView that in my app that is populated via SQLitle db from cursor.. looks like this:

public void updateShiftList(String x) {
    db.openToRead();

    listShifts = (ListView) findViewById(R.id.listShifts);

    // get data

    cursor = db.getPlan(x);
    startManagingCursor(cursor);
    // set adapter
    String[] from = { dataManager.KEY_SHIFT, dataManager.KEY_DATE };
    int[] to = { R.id.rowShift, R.id.rowDate };

    adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

    listShifts.setAdapter(adapter);
    db.close();

}

in row.xml I have image before each row and I would like to implement function or check method during creation of this ListView that would change the image depending on some parameters that I would test, eg R.id.rowDate == TodayDate ( there is date in that text field ) or if the R.id.rowDate is next date from today....

How could I do that, or where... is there some Overide that could be used ? I'm learning java on this simple project as I'm doing it so be gentle :)

Thanks all, Vlad

2 Answers 2

1

And again...

Build a custom Adapter that uses your data...

Whenever you want to do processing with the views in a ListView you need to create a custom adapter that will handle your logic implementation and pass that information to the views as necessary.

Example

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

Sign up to request clarification or add additional context in comments.

Comments

0

Below is an excerpt from my code. I use it in many projects of mine, that collect data from database and display them in a ListView. It works. You my need to adapt it slightly to your project. Anyway I would recommend to rewrite your code. Image stuff is at the bottom:

package my.package;

import android.app.Activity;


public class MyActivity extends Activity {


private DBAdapter mDb;

private ListView mListView;
private Cursor mCursor;   



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.groups_list);       

    mDb = new DBAdapter(this);
    mDb.open();       

    populateListView();
}    

@Override
public void onDestroy() {
    super.onDestroy();

    if(mCursor != null)
        mCursor.close();
    mCursor = null;

    if(mDb != null)
        mDb.close();        
    mDb = null;       
}    


private void populateListView(){        

    if(mCursor!=null)  //close the old one first
        mCursor.close();
    mCursor = mDb.getSomeDataCursor();                

    MainAdapter adapter = new MainAdapter(this, R.layout.groups_list_item, mCursor, false);        
    mListView.setAdapter(adapter);
}    


 private class MainAdapter extends ResourceCursorAdapter{

    private int mLayout;
    private int mColumnIndexGroup;

    public MainAdapter(Context pContext, int pLayout, Cursor pCursor, boolean pAutoRequery) {
        super(pContext, pLayout, pCursor, pAutoRequery);

        mLayout = pLayout;
        mColumnIndexGroup = (pCursor.getColumnIndex(DBAdapter.COL_NAME)); 
    }        

    @Override
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) {
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return li.inflate(mLayout, pParent, false);
    }        

    @Override
    public void bindView(View pView, Context pContext, Cursor pCursor) {           
        TextView tvGroup = (TextView)pView.findViewById(R.id.groupsListTVGroup);
        tvGroup.setText(pCursor.getString(mColumnIndexGroup));

        //Image stuff - an example
        if(pCursor.getInt(pCursor.getColumnIndex(SOMETHING)) == SOME_VAL){
            ImageView img = (ImageView) pView.findViewById(R.id.myImage);
            img.setImageResource(R.drawable.myImage)
        }
   }     

}

}

3 Comments

Thanks, I see now. I didn't know you can make your own adapter.
I managed to make it work ! One more question what for is that 'newView' Override ? In many examples I have seen that there is almost the same code as in 'bindView' I even try to use it without it and it worked... not sure what is it for. I would be very grateful for simple explanation, thanks.
From what I see in the official docs, newView is to inflate views from XML (and in simple cases might be not needed as you noticed, I am not sure), and bindView is to put data from the cursor to the views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.