Display text and image from database with SimpleCursorAdapter.ViewBinder

1.9k views Asked by At

I show the data from database like text and image, but my code is not working. I use this tutorial for my project. This my java class:

public class PepakPostView extends ListActivity {
    protected TextView postOne;
    protected TextView postTwo;
    //protected view picture;
    protected ListAdapter adapter;
    protected int subcatId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post);
        subcatId = getIntent().getIntExtra("SUBCATEGORY_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, subcatId, postOne, postTwo, picture FROM posts WHERE subcatId = ?", 
                new String[]{""+subcatId});

        SimpleCursorAdapter pepak = new SimpleCursorAdapter(
                this,
                R.layout.post_list, 
                cursor, 
                new String[] {"postOne", "postTwo", "picture"}, 
                new int[] { R.id.postOne, R.id.postTwo, R.id.picture });

            pepak.setViewBinder(new MyViewBinder());   

    }


     @Override
        public boolean onCreateOptionsMenu(android.view.Menu menu) {
            // TODO Auto-generated method stub
            super.onCreateOptionsMenu(menu);
            MenuInflater blowUp =  getMenuInflater();
            blowUp.inflate(R.menu.coll_menu, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
         switch(item.getItemId()){
         case R.id.about:
             Intent i = new Intent("com.pepakbahasajawa.ABOUT");
             startActivity(i);
             break;
         case R.id.exit:
                finish();
                break;
         }
         return false;
        }

}

and MyViewBinder.java:

public class MyViewBinder implements ViewBinder {

    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId) {

        /*
        case R.id.postOne:

                TextView postOne = (TextView) view;
                postOne.setText(Cursor.getString(columnIndex));

            break;

            case R.id.postTwo:

                TextView postTwo = (TextView) view;
                postTwo.setText(Cursor.getString(columnIndex));

            break;
 */
            case R.id.picture:

                ImageView pictureIcon = (ImageView) view;
                int picture = cursor.getInt(columnIndex);
                switch(picture) {
                    case 1:
                        pictureIcon.setImageResource(R.drawable.icon);
                    break;
                    case 2:
                        pictureIcon.setImageResource(R.drawable.home_normal);
                    break;
                }

            break;
        }
        return false;
    }
}

How to fix this?

1

There are 1 answers

0
Deucalion On

pepak variable is not used in ListView. ListView is empty.

setListAdapter(pepak);//?