我目前有一个ListView,由SimpleAdapter填充,人口正常。我遇到的问题是我似乎无法弄清楚如何在onCreate期间更改ListItem LinearLayout的背景颜色。如果我单击一个按钮或精心做一个动作但是如果我在onCreate中执行它,或者如果我在基于我的SQLITE数据库中的所有表填充ListView后执行此操作,它工作得很好。
此代码正在改变颜色。
if(c.moveToFirst()){
do{
if(checkCat.moveToFirst()){
do{
if(c.getString(0).equals(checkCat.getString(0))){
for (int i = 0; i < adapter.getCount(); i++) {
final FrameLayout listItemLayout = (FrameLayout) cardList.getChildAt(i);
final LinearLayout lL = (LinearLayout) listItemLayout.findViewById(R.id.cardmainlayout);
final TextView tv = (TextView) listItemLayout.findViewById(R.id.text1);
if(tv.getText().toString().equals(checkCat.getString(1).trim())) {
switch (c.getString(1).trim()) {
case "White":
lL.setBackgroundResource(R.drawable.bg_color_white);
break;
case "Orange":
lL.setBackgroundResource(R.drawable.bg_color_orange);
break;
case "Red":
lL.setBackgroundResource(R.drawable.bg_color_red);
break;
case "Green":
lL.setBackgroundResource(R.drawable.bg_color_green);
break;
case "Purple":
lL.setBackgroundResource(R.drawable.bg_color_purple);
break;
case "Blue":
lL.setBackgroundResource(R.drawable.bg_color_blue);
break;
}
switch (c.getString(2).trim()) {
case "White":
tv.setTextColor(getResources().getColor(android.R.color.white));
break;
case "Black":
tv.setTextColor(getResources().getColor(android.R.color.black));
break;
case "Green":
tv.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
break;
case "Orange":
tv.setTextColor(getResources().getColor(android.R.color.holo_orange_dark));
break;
case "Red":
tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
break;
case "Blue":
tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
break;
}
}
}
}
}while (checkCat.moveToNext());
}
}while(c.moveToNext());
}
这段代码填充了ListView
if (c.moveToFirst()) {
do {
if (!c.getString(0).contains("a")) {
if (!c.getString(0).contains("name")) {
if (!c.getString(0).contains("name2")) {
String str = c.getString(0);
map = new HashMap<String, Object>();
map.put("title", str.toString();
fillMaps.add(map);
}
}
}
} while (c.moveToNext());
}
还有这个代码重新更新ListView基于类别代码在这里工作。问题是第一部分与上面相同所以它崩溃了,第二部分在adpater刷新后没有完全更新。你必须再次单击它的类别按钮才能检测到适配器已更改。(是的,我正在调用adapter.notifyDataSetChanged()。)
if(strName.toLowerCase().equals("all")) {
fillMaps.clear();
Cursor c = appDB.rawQuery("SELECT name FROM sqlite_master WHERE type='table';", null);
if (c.moveToFirst()) {
do {
if (!c.getString(0).contains("android_metadata")) {
if (!c.getString(0).contains("Name1")) {
if (!c.getString(0).contains("Name")) {
map = new HashMap<String, Object>();
map.put("title", c.getString(0));
fillMaps.add(map);
chkBox = 0;
}
}
}
} while (c.moveToNext());
}
}
else {
Cursor checkCat = appDB.rawQuery("SELECT * FROM Name", null);
if (checkCat.moveToFirst()) {
fillMaps.clear();
do {
if (checkCat.getString(0).contains(strName)) {
map = new HashMap<String, Object>();
map.put("title", checkCat.getString(1));
fillMaps.add(map);
chkBox = 0;
}
} while (checkCat.moveToNext());
}
}
adapter.notifyDataSetChanged();
最后但并非最不重要的是这是我的SimpleAdapter
adapter = new SimpleAdapter(this, fillMaps, R.layout.main_c_layout, from, to) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rRow = super.getView(position, convertView, parent);
final Button vsButton = (Button) rRow.findViewById(R.id.quizListViewButton);
final CheckBox cb = (CheckBox) rRow.findViewById(R.id.deleteCheckBox);
TextView v = (TextView) rRow.findViewById(R.id.text1);
final String str = v.getText().toString().replaceFirst("\\s+$", "");
vsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, str + "", Toast.LENGTH_SHORT).show();
}
});
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb.isChecked()) {
chkBox++;
} else {
chkBox--;
}
if (chkBox > 1) {
editNoteSet.setVisibility(View.GONE);
} else {
editNoteSet.setVisibility(View.VISIBLE);
}
}
});
return rRow;
}
};
List.setAdapter(adapter);
所以只是为了清楚我的问题,你将如何更改位于Custom ListView行中的LinearLayout的颜色。(对于记录,所有内容都正确填充,我只是无法更改颜色。)
此外,我一直得到的错误是尝试在空对象引用上调用虚方法'android.view.View android.widget.FrameLayout.findViewById(int)' 这没有意义,因为当我点击一个按钮或进行物理操作时它可以工作。感谢您提供的任何和所有帮助。
感谢Mehrdad1373,我能够提出解决方案。所以解决方案是实际更改SimpleAdapter getView方法中的背景。我没有意识到每次删除或添加行时都会调用简单的适配器。所以这就是我做的..
adapter = new SimpleAdapter(this, fillMaps, R.layout.main_c_layout, from, to) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rRow = super.getView(position, convertView, parent);
final Button vsButton = (Button) rRow.findViewById(R.id.quizListViewButton);
final CheckBox cb = (CheckBox) rRow.findViewById(R.id.deleteCheckBox);
LinearLayout lL = (LinearLayout)rRow.findViewById(R.id.myLayout);
TextView v = (TextView) rRow.findViewById(R.id.text1);
final String str = v.getText().toString().replaceFirst("\\s+$", "");
If(str.equals(c.getString(0)){
lL.setBackgroundColor(Color.Blue);
}else{
lL.setBackgroundColor(Color.Green);
}
vsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, str + "", Toast.LENGTH_SHORT).show();
}
});
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb.isChecked()) {
chkBox++;
} else {
chkBox--;
}
if (chkBox > 1) {
editNoteSet.setVisibility(View.GONE);
} else {
editNoteSet.setVisibility(View.VISIBLE);
}
}
});
return rRow;
}
};
List.setAdapter(adapter);
答案 0 :(得分:0)
您应该将此代码放在SimpleAdapter中的getView()中 以下是您需要做的事情:
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
rowView.setBackgroundColor(Color.BLUE);//You Can define your own color by using Color.argb()
请注意,mylist是定义列表内容的xml布局的名称
答案 1 :(得分:0)
尝试调用更改OnGlobalLayoutListener
listView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (!Build.JELLY_BEAN_COMPATIBILITY) {
listView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} else {
listView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
yourFunctionHere();
}
});
}