在 android 中的按钮中显示从数据库中获取的数据

Displaying data fetched from database in a Button in android

我知道如何从 sqlite 数据库中获取数据并将其显示在列表视图中。但我想知道如何根据从数据库中获取的数据(如颜色、文本等)更改按钮的属性。 我搜索了很多但找不到任何相关答案。 是不是我们只能在列表视图中显示来自数据库的数据?

更新:我修改了我的代码以在列表视图中显示数据,如下所示:

    public class TextAdapter extends BaseAdapter {

    public ArrayList<Integer>  arr_Id = new ArrayList<Integer>();
    public ArrayList<String>  arr_Name = new ArrayList<String>();
    public ArrayList<String>  arr_Status = new ArrayList<String>();
    public ArrayList<String>  arr_Color = new ArrayList<String>();

    public int i = 0;
    private Context mContext;

    public  TextAdapter(Context c,ArrayList<String> array_Name, ArrayList<String> array_Status, ArrayList<String> array_Color) {

        mContext = c;

        arr_Name = array_Name;
        arr_Status = array_Status;
        arr_Color = array_Color;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr_Name.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub      

        String status;

        TextView mTextView;
        if (convertView == null) {
            mTextView = new TextView(mContext);
        }

        else {
            mTextView = (TextView)convertView;
        }
        mTextView.setText(arr_Name.get(position) +" "+  arr_Status.get(position) );

        position = 0;

        while (position < arr_Status.size()) {
            status = arr_Status.get(position);
            if (status.equals("Finnished")) {
                mTextView.setBackgroundResource(color.holo_green_dark);
            }
            if (status.equals("Running")) {
                mTextView.setBackgroundResource(color.holo_orange_dark);
            }
            if (status.equals("Stopped")) {
                mTextView.setBackgroundResource(color.holo_red_dark);
            }
            position = position + 1;
//          status = null;
        }

        return mTextView;
    }

}

并且输出是:获取的数据显示在列表视图中。每行都有进程名称及其状态(运行、完成和停止)。 现在我希望完成过程的行颜色为绿色,停止为红色,运行 为橙色。这里第 1 行和第 3 行的颜色不符合要求。 注意:每一行都是红色的。

请帮忙。

这不起作用的原因是因为您正在以一种适用于一个的方法循环遍历所有项目。

getView() 方法对 ListView 的每一行执行一次,这意味着您应该只查看一个状态。

请注意,当您设置状态文本时,您正在查看给定位置的状态。你也需要为颜色做这件事。尝试将行更改为如下内容:

String status = arr_Status.get(position);

mTextView.setText(arr_Name.get(position) + " " + status;

if(status.equals("Finished"){
   mTextView.setBackgroundResource(color.holo_green_dark);
} else if(status.equals("Running"){
   mTextView.setBackgroundResource(color.holo_orange_dark);
} else{ // Stopped
   mTextView.setBackgroundResource(color.holo_red_dark);
}

position 参数表示您要显示的行,因此无需像那样遍历每一行。

出于这个原因,我假设最后一个位置的状态为停止,这就是为什么每一行都是红色的,因为在每一行你都在浏览列表,并且状态颜色设置为列表中的最后一项。