I have a question/answer app. I want to change the listItem color is that question is answered by that author.Here is a snippet
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
QuestionGetSet movie = new QuestionGetSet();
movie.setQues(obj.getString("abc"));
movie.setDate(obj.getString("def"));
movie.setNumber(obj.getString("ghi"));
farmerName[i] = obj.getString("jkl");
cropType[i] = obj.getString("mno");
relatedField[i] = obj.getString("pqr");
number[i]=obj.getString("stu");
// adding movie to movies array
movieList.add(movie);
if(i==0) {
listView.getChildAt(i).setBackgroundColor(Color.GREEN);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
The Problem is that the program is giving NullPointerException at
if(i==0) {
listView.getChildAt(i).setBackgroundColor(Color.GREEN);
}
Adapter Code:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<QuestionGetSet> quesItems;
public CustomListAdapter(Activity activity, List<QuestionGetSet> quesItems) {
this.activity = activity;
this.quesItems = quesItems;
}
@Override
public int getCount() {
return quesItems.size();
}
@Override
public Object getItem(int location) {
return quesItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_list_layout_home, null);
TextView question = (TextView) convertView.findViewById(R.id.txt_questions);
TextView date = (TextView) convertView.findViewById(R.id.txt_date);
TextView number = (TextView) convertView.findViewById(R.id.txt_asked_by);
// getting movie data for the row
QuestionGetSet m = quesItems.get(position);
// title
question.setText(m.getQues());
// rating
date.setText(m.getDate());
// release year
number.setText(m.getNumber());
return convertView;
}
}
I hope i am clear.Please help.
This is because you call listView.getChildAt(i)
before listview is actually set. Try to change background color getView()
in adapter.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_list_layout_home, null);
TextView question = (TextView) convertView.findViewById(R.id.txt_questions);
TextView date = (TextView) convertView.findViewById(R.id.txt_date);
TextView number = (TextView) convertView.findViewById(R.id.txt_asked_by);
// getting movie data for the row
QuestionGetSet m = quesItems.get(position);
// title
question.setText(m.getQues());
// rating
date.setText(m.getDate());
if (position == 0) {
convertView.setBackgroundColor(Color.GREEN);
}
return conertView;
}
Hope It will be useful for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.