简体   繁体   English

更改Android的ListView文本

[英]Change listview text Android

I have a SQLite database that is converted into a listview and everything is working fine. 我有一个SQLite数据库,该数据库已转换为列表视图,并且一切正常。 But i am facing a problem now: 但是我现在面临一个问题:

I have a column named stage in my database. 我的数据库中有一列名为stage的列。 Stage will contain one of these integers: 0, 1 or 2. In the layout file of the listview items i have a textview called status, and the value of stage is printed into this textview. Stage将包含以下整数之一:0、1或2。在listview项的布局文件中,我有一个名为status的textview,并且stage的值已打印到此textview中。 But i don't want 0, 1 or 2 printed in the textview, i want to replace the integer with a word. 但是我不想在textview中打印0、1或2,我想用一个单词替换整数。

So for example if the value of stage is 0, i want the content of status to be "hello". 因此,例如,如果stage的值为0,则我希望状态的内容为“ hello”。 And if the value of stage is 1, i want the content of status to be "goodmorning". 如果stage的值为1,则我希望状态的内容为“早安”。 I have tried multiple things but i don't get it working. 我已经尝试了多种方法,但是我无法正常工作。

The code of the listview: listview的代码:

private DBManager dbManager;

private ListView listView;

private SimpleCursorAdapter adapter;

final String[] from = new String[] { DatabaseHelper._ID,
        DatabaseHelper.IDEA, DatabaseHelper.DESC, DatabaseHelper.STAGE};

final int[] to = new int[] { R.id.id, R.id.idea, R.id.desc, R.id.status };

dbManager = new DBManager(this);
dbManager.open();
Cursor cursor = dbManager.fetch();

listView = (ListView) findViewById(R.id.list_view);
listView.setEmptyView(findViewById(R.id.empty));

adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
adapter.notifyDataSetChanged();

listView.setAdapter(adapter);

I hope somebody can help me, thanks in advance :). 我希望有人能帮助我,在此先感谢:)。

Try like this 这样尝试

Take 采取

int i;

Then check 然后检查

if (i==0) {
    textview.setTe xt("hello");
} else if (i==1) {
    textview.setText("good morning");
}

You could implement a custom adapter rather than using SimpleCursorAdapter and then change/convert the value in the getView or bindView method within the adapter. 您可以实现自定义适配器,而不是使用SimpleCursorAdapter,然后在适配器内的getViewbindView方法中更改/转换值。

Alternately you could change/convert the values via the query that returns the cursor. 或者,您可以通过返回游标的查询来更改/转换值。 Along the lines of :- 遵循以下原则:

SELECT col1, col2,  (CASE WHEN colx = 0 THEN 'Hello' ELSE 'GoodMorning' END) AS mynewcolum FROM TABLE;

Where col1, col2 and colx are existing columns and colx is the column that is to be converted. 其中col1,col2和colx是现有列,而colx是要转换的列。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM