I currently have a ListView Which is being populated by a SimpleAdapter and the population is working great. Problem I am having is I cant seem to figure out how to change the background color of the ListItem LinearLayout during the onCreate. It is working perfectly fine if I click a button or do an action psychically but does not want to work if i do it in the onCreate or if I do it after populating the ListView based off of all the tables in my SQLITE database.
This code is what is changing the color.
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());
}
This code is what is populating the 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());
}
Also have this code which reupdates the ListView Based on categories the code is sort of working here. Problem is the first part is the same as above so it crashes and the second part does not exactly update after the adpater is refreshed. you have to click it the category button again for it to detect the adapter changed.(yes I am calling the 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();
and last but not least this is my 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);
So just to clear my question up how would you change the color of the LinearLayout that is located within the Custom ListView row.(For the record everything is populating properly I just cannot change the color.)
Also, The error I keep getting is Attempt to invoke virtual method 'android.view.View android.widget.FrameLayout.findViewById(int)' on a null object reference which makes not sense because it works when I click a button or do an action physically. Any and all help is appreciated Thank you in advance.
Solution:
Thanks to Mehrdad1373 I was able to come up with the solution. So the solution was to actually change the background in the SimpleAdapter getView method. I didn't realize that the simple adapter was getting called everytime a row was removed or added. So this is what I did..
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);