具有JSONArray的自定义基本适配器上的notifydatasetchanged()

时间:2014-03-07 06:28:19

标签: android notifydatasetchanged

我创建了一个自定义基类适配器类,我在其中传递了activity和JSONArray的上下文。我使用此JSONArray数据来设置所有视图。但适配器上的notifyDataSetChanged()无法正常工作。我是否必须在适配器中传递字符串数组或字符串的arraylist,以使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;
}
}

2 个答案:

答案 0 :(得分:1)

你必须更新arrayOfMessages的另一个对象在适配器中创建一个函数,如:

public void setArrayOfMessages(JSONArray arrayOfMessages){
this.arrayOfMessages=arrayOfMessages;
}

并在致电notifyDataSetChanged之前:

adapter.setArrayOfMessages(object);
adapter.notifyDataSetChanged();

答案 1 :(得分:0)

最好将Json Array中的数据转换为Arraylist或List,然后应用notifyDataSetChanged。