我制作了一个自定义的基本适配器类,在其中传递了活动的上下文和一个 JSONArray。我使用这个 JSONArray 数据来设置所有视图。但是适配器上的 notifyDataSetChanged() 不起作用。我是否必须在适配器中传递字符串数组或字符串数组列表才能使 notifyDataSetChanged() 工作?如果我通过 JSONArray,它会不起作用吗?
我的自定义适配器类代码:
public class InboxMessagesAdapter extends BaseAdapter {
Activity act;
SharedPreferences prefs;
LayoutInflater Inflater;
JSONArray arrayOfMessages;
public InboxMessagesAdapter(Activity a, JSONArray arry) {
act = a;
arrayOfMessages = arry;
prefs = PreferenceManager.getDefaultSharedPreferences(a);
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayOfMessages.length();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v1 = arg1;
try {
final int x = arg0;
JSONObject js = arrayOfMessages.getJSONObject(arg0);
v1 = Inflater.inflate(R.layout.element_inbox, null);
TextView titleOfMessage = (TextView) v1
.findViewById(R.id.msg_inbox_title);
TextView timeOfMessage = (TextView) v1
.findViewById(R.id.msg_inbox_time);
final CheckBox selectMessage = (CheckBox) v1
.findViewById(R.id.msg_inbox_check_button);
ImageView attachment = (ImageView) v1
.findViewById(R.id.msg_inbox_attachment);
ImageView starStatus = (ImageView) v1
.findViewById(R.id.msg_inbox_addtofav);
if (js.getString("StarStatus").equals("0"))
starStatus.setImageResource(R.drawable.btn_add_to_fav_normal);
else
starStatus.setImageResource(R.drawable.btn_add_to_fav_pressed);
if (js.getString("AttachmentStatus").equals("0"))
attachment.setVisibility(View.GONE);
titleOfMessage.setText(js.getString("MessageSubject"));
String arr[] = js.getString("MessageDate").split(" ");
try {
timeOfMessage.setText(arr[1] + arr[2]);
} catch (Exception e) {
timeOfMessage.setText(js.getString("MessageDate"));
}
v1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
Intent in = new Intent(act, MessageActivity.class);
in.putExtra("data", arrayOfMessages.getJSONObject(x)
.toString());
act.startActivity(in);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return v1;
}
}