2
已经淘到这个网站的任何答案,没有真正简单的解决方案,我找到了这个。我正在创建一个Android应用程序,它使用sqlite数据库通过输入的颜色名称查找十六进制值。我动态创建一个TextView,设置其文本和文本颜色,然后将其添加到ArrayList,然后ArrayList正在添加到ListView中。该文本显示在ListView中,但其颜色属性未设置。我真的很想找到一种方法来为每个listview项目设置文本颜色。这里是我的代码至今:在一个listview项目中设置文本视图的文本颜色? (Android)
类变量:
private ListView lsvHexList;
private ArrayList<String> hexList;
private ArrayAdapter adp;
在的onCreate():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color2hex);
lsvHexList = (ListView) findViewById(R.id.lsvHexList);
hexList = new ArrayList<String>();
在我的按钮处理程序:
public void btnGetHexValueHandler(View view) {
// Open a connection to the database
db.openDatabase();
// Setup a string for the color name
String colorNameText = editTextColorName.getText().toString();
// Get all records
Cursor c = db.getAllColors();
c.moveToFirst(); // move to the first position of the results
// Cursor 'c' now contains all the hex values
while(c.isAfterLast() == false) {
// Check database if color name matches any records
if(c.getString(1).contains(colorNameText)) {
// Convert hex value to string
String hexValue = c.getString(0);
String colorName = c.getString(1);
// Create a new textview for the hex value
TextView tv = new TextView(this);
tv.setId((int) System.currentTimeMillis());
tv.setText(hexValue + " - " + colorName);
tv.setTextColor(Color.parseColor(hexValue));
hexList.add((String) tv.getText());
} // end if
// Move to the next result
c.moveToNext();
} // End while
adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hexList);
lsvHexList.setAdapter(adp);
db.close(); // close the connection
}
听起来不错。这看起来代码明智吗?这里有一个新手程序员。 = P –
我现在没有ListAdapter示例,但请参阅下面的链接来扩展BaseAdapter,这个想法几乎相同:https://github.com/BinyaminSharet/Icelandic-Memory-Game/blob/master /src/com/icmem/game/BoardGridAdapter.java – MByD
不完全有帮助。还有谁? –