我正在尝试将一些消息(我从API调用中获取)加载到ListView上。消息在AsyncTask中加载和检索,但我发布的是来自doInBackground()方法。每当我测试应用程序时,ListView都是空的......我该如何解决这个问题?
for(int x = 0; x < array.length; x++){
String user = "User: " + array[x].getName();
String subject = " Subject: " + array[x].getSubject();
String body = " Body: " + array[x].getBody();
String message = user + subject + body;
aList.add(message);
}
//for (int i = 0; i < mess.length; ++i) {
//aList.add(mess[i]);
//}
mess = aList.toArray(new String[aList.size()]);
return true;
}
protected void onPostExecute(Boolean results){
Adapter adapter = new Adapter(ctx,mess);
listview.setAdapter(adapter);
}
}
private class Adapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public Adapter(Context context, String[] values) {
super(context, R.layout.messages, values);
this.context = context;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.messages, parent, false);
return rowView;
}
}
答案 0 :(得分:1)
在getView()方法中,您没有将值设置为任何值。假设您在R.layout.messages布局中定义了textview,那么您需要检索它并设置文本。
这样的事情:
View rowView = inflater.inflate(R.layout.messages, parent, false);
TextView txtView = rowView.findViewById(R.id.mytextview);
txtView.setText(values[position]);