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

发布于 2024-12-09 15:12:47 字数 3101 浏览 0 评论 0原文

我显示数据库中的数据(例如文本和图像),但我的代码无法正常工作。我在我的项目中使用本教程。这是我的 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;
        }

}

和 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;
    }
}

如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2024-12-16 15:12:47

ListView 中不使用 pepak 变量。列表视图为空。

setListAdapter(pepak);//?

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

setListAdapter(pepak);//?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文