如何改变颜色/特定的一个的ListView行的图片 - Android的? [英] How change color/picture of specific one ListView row - Android?

查看:192
本文介绍了如何改变颜色/特定的一个的ListView行的图片 - Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据具体情况来改变只有一个(也许更多)的ListView一行。我看过类似的问题,并吨的其他教程很多答案,但我不能做任何事情。

I am trying to change only one (maybe more) ListView row based on specific condition. I have read many answers on similar questions and tons of other tutorials but I am not able to make anything.

正是我想要实现的是具有行背景(更简单的版本)或行图像时,从SQLite的行设置为特定的值设置为比别人不同。(我想象的更难版本)

Exactly what I want to achieve is to have row background (easier version) or row picture (I think harder version) set different than others when row from SQLite is set at specific value.

我有一个扩展ListActivity和我设置在ListView适配器这样的活动:

I have got Activity that extends ListActivity and I am setting the ListView adapter like this:

private void refreshList() {
    mySQLiteAdapter = new MyDBAdapter(this);
    mySQLiteAdapter.open();
    String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_GENRE,
            MyDBAdapter.KEY_PRICE, MyDBAdapter.KEY_ID };

    Cursor contentRead = mySQLiteAdapter.getAllEntries(false, columns,
            null, null, null, null, MyDBAdapter.KEY_TITLE, null);

    SimpleCursorAdapter adapterCursor = new SimpleCursorAdapter(this,
            R.layout.row, contentRead, columns, new int[] {
                    R.id.text1, R.id.detail });

    this.setListAdapter(adapterCursor);
    mySQLiteAdapter.close();
}

此函数被调用onCreate方法和onResume。我想设置的行不同的颜色/图像从哪里列的值 MyDBAdapter.KEY_PRICE 等于5。R.layout.row是排设计我的XML文件。

This function is called in onCreate method and in onResume. I want to set different color/image of row where value from column MyDBAdapter.KEY_PRICE is equal to 5. The R.layout.row is my xml file with row design.

也许有人可以帮助我?或者至少说明教程描述它?

Maybe someone can help me with this? Or at least show tutorial describing it?

推荐答案

简单地扩展SimpleCursorAdapter并覆盖bindView():

Simply extend SimpleCursorAdapter and override bindView():

public class MyAdapter extends SimpleCursorAdapter {
    public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
            view.setBackgroundColor(0xff00ff00);
        else // this will be the default background color: transparent
            view.setBackgroundColor(0x00000000);
    }
}

这篇关于如何改变颜色/特定的一个的ListView行的图片 - Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆