简体   繁体   English

使用SimpleCursorAdapter.ViewBinder显示数据库中的文本和图像

[英]Display text and image from database with SimpleCursorAdapter.ViewBinder

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: 这是我的java类:

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 : 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? 如何解决这个问题?

pepak variable is not used in ListView. pepak变量未在ListView中使用。 ListView is empty. ListView为空。

setListAdapter(pepak);//? setListAdapter(pepak); //?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SimpleCursorAdapter.ViewBinder文本未显示 - SimpleCursorAdapter.ViewBinder text not showing 使用SimpleCursorAdapter.ViewBinder更改Listview中的文本颜色 - Changing text color in Listview using SimpleCursorAdapter.ViewBinder Android - SimpleCursorAdapter.ViewBinder - 设置背景颜色 - Android - SimpleCursorAdapter.ViewBinder - Set background color Android SimpleCursorAdapter.ViewBinder不更新绑定的TextView - Android SimpleCursorAdapter.ViewBinder not updating bound TextView 使用SimpleCursorAdapter.ViewBinder更改TextView的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of TextView 使用SimpleCursorAdapter.ViewBinder更改视图的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of View SimpleCursorAdapter.ViewBinder-不同地应用于每一行(ListFragment) - SimpleCursorAdapter.ViewBinder - Apply differently to each row (ListFragment) 使用SimpleCursorAdapter.ViewBinder更改listView中的颜色。 安卓 - Using SimpleCursorAdapter.ViewBinder to change color in a listView. android 使用SimpleCursorAdapter和ViewBinder在ListView项目中设置图像 - Setting an image in ListView item using SimpleCursorAdapter and ViewBinder 使用ViewBinder从SimpleCursorAdapter中显示的自定义列表 - Customizing list shown from SimpleCursorAdapter using ViewBinder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM