gpt4 book ai didi

android - ListView项目背景:Android

转载 作者:行者123 更新时间:2023-11-29 01:28:33 25 4
gpt4 key购买 nike

我有一个问答应用。我想更改 listItem 的颜色是那个作者回答了这个问题。这是一个 fragment

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();
}
}

问题是程序在

处给出 NullPointerException
if(i==0) {
listView.getChildAt(i).setBackgroundColor(Color.GREEN);
}

适配器代码:

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;
}

我希望我清楚。请帮忙。

最佳答案

这是因为您在实际设置 ListView 之前调用了 listView.getChildAt(i)。尝试在适配器中更改背景颜色 getView()

@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;
}

希望对你有用。

关于android - ListView项目背景:Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com