在android中使用自定义适配器填充listView
我正在尝试使用片段的自定义适配器创建一个包含消息的listview。我的android项目运行时没有问题或错误。我可以看到消息被正确地传递到构造函数中。我不认为我的getView会被调用,我相信它会自动发生。我在适配器的getView中添加了指纹,但它们永远不会消失 这是我的MessageAdapter在android中使用自定义适配器填充listView,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,我正在尝试使用片段的自定义适配器创建一个包含消息的listview。我的android项目运行时没有问题或错误。我可以看到消息被正确地传递到构造函数中。我不认为我的getView会被调用,我相信它会自动发生。我在适配器的getView中添加了指纹,但它们永远不会消失 这是我的MessageAdapter public class MessageAdapter extends ArrayAdapter<message> { private final Context context
public class MessageAdapter extends ArrayAdapter<message> {
private final Context context;
private final int layoutResourceId;
private final ArrayList<message> messages;
public MessageAdapter(Context context, int layoutResourceId, ArrayList<message> messages) {
super(context, layoutResourceId);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.messages = messages;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View row = convertView;
ViewHolder holder = null;
int direction = messages.get(i).userType;
//show message on left or right, depending on if
//it's incoming or outgoing
System.out.println("try this");
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
int res = 0;
if (direction == 0) {
res = R.layout.message_right;
} else if (direction == 1) {
res = R.layout.message_left;
}
row = inflater.inflate(res, viewGroup, false);
holder = new ViewHolder();
holder.txtMessage = (TextView) row.findViewById(R.id.txtMessage);
holder.txtDate = (TextView) row.findViewById(R.id.txtDate);
row.setTag(holder);
}
System.out.println(messages.get(i).message);
holder.txtMessage.setText(messages.get(i).message);
holder.txtDate.setText(messages.get(i).time);
return row;
}
static class ViewHolder {
TextView txtMessage;
TextView txtDate;
}
}
这里是我在片段中调用适配器的地方
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View messagesView = inflater.inflate(R.layout.messages, container, false);
ListView listView = (ListView)messagesView.findViewById(R.id.listMessages);
ArrayList<message> messages = getMessages();
MessageAdapter adapter = new MessageAdapter(getActivity(), R.id.listMessages, messages);
listView.setAdapter(adapter);
...
编辑:
根据请求获取消息
String[] messageArray = getMessages(); // get messages returns an array of json encoded rows from the db
for(int i=0; i<messageArray.length; i++) {
String theMess = null, time = null;
int userType = 0;
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(messageArray[i]);
} catch (JSONException e) {
e.printStackTrace();
}
try {theMess = jsonObject.getString("message");} catch (JSONException e) {}
try {time = jsonObject.getString("time");} catch (JSONException e) {}
try {userType = jsonObject.getInt("userType");} catch (JSONException e) {}
message newMessage = new message(theMess, time, userType);
messages.add(newMessage);
}
您缺少getSize方法。添加getsize和getItem方法后,这将按预期工作并且您必须在getview方法中将视图添加到viewgroup。更改
super(context, layoutResourceId);
到
您不需要getCount或getItem。除非您需要自己的特殊逻辑,否则ArrayAdapter已经为它们提供了应该可以工作的实现。您不需要ArrayList消息,ArrayAdapter已经维护了自己的ArrayList。您应该将消息传递给超级ctr,并在getView方法中使用getItemposition。您可能应该使用Logs而不是System.out,并且仍然应该尝试使用断点。发布您的getMessages方法,在帖子中编辑。getMessages函数返回一个有效的消息数组。我刚刚添加了两个:public int getSize{return messages.size;}public message getItemint I{return messages.geti;},我仍然得到一个空屏幕。有没有具体的方法?
super(context, layoutResourceId, messages);