Android更新适配器无法正常运行

时间:2014-10-09 08:02:19

标签: android

在此代码中,我有一个自定义适配器,在我将新数据添加到ArrayListadapter.notifyDataSetChanged();我的public class ReceiveListFragment extends ListFragment implements AbsListView.OnScrollListener { public ArrayList<ReceivedItemStructure> items; private int prevVisibleItem; private boolean isFirstTime; private DatabaseHandler db; private String config_username; private String config_password; private ReceivedAdapter adapter; private Cursor cursor; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { db = new DatabaseHandler(G.context); config_username = Configuration.getInstance() .getString(getActivity(), Configuration.SharedPrefsTypes.USERNAME); config_password = Configuration.getInstance() .getString(getActivity(), Configuration.SharedPrefsTypes.PASSWORD); items = new ArrayList<ReceivedItemStructure>(); getRequestFromServer(0, 10); adapter = new ReceivedAdapter(G.context, items); setListAdapter(adapter); Timer smsThread = new Timer(); GetSMSThread getSMSThread = new GetSMSThread(); smsThread.scheduleAtFixedRate(getSMSThread, 1, 10000); //(timertask,delay,period) return super.onCreateView(inflater, container, savedInstanceState); } private String getRequestFromServer(long lastID, int count) { String received = ""; try { received = new JsonService(config_username, config_password, lastID, count, G.F_RECEIVE_SMS).request(); JSONArray data_array = new JSONArray(received); String mUserID = config_username; for (int i = 0; i < data_array.length(); i++) { JSONObject json_obj = data_array.getJSONObject(i); String mLastID = json_obj.getString("id_recived_sms"); String mSmsBody = json_obj.getString("sms_body"); String mSmsNumber = json_obj.getString("sms_number"); String mSenderName = json_obj.getString("mobile_number"); String mContactName = json_obj.getString("contact_name"); String mDate = json_obj.getString("recived_date"); ReceivedItemStructure item = new ReceivedItemStructure( mLastID, mUserID, mSmsBody, mSmsNumber, mSenderName, mContactName, mDate ); items.add(item); //Log.e(" ", "" + mLastID); } /** Creating array adapter to set data in listview */ } catch (Exception e) { e.printStackTrace(); } return received; } public long getLastID() { return Long.parseLong(items.get(items.size() - 1).getmLastID()); } private void addDataToList(String LastID, String SmsBody, String SmsNumber, String SenderName, String ContactName, String Date) { String mLastID = LastID; String mUserID = config_username; String mSmsBody = SmsBody; String mSmsNumber = SmsNumber; String mSenderName = SenderName; String mContactName = ContactName; String mDate = Date; ReceivedItemStructure item = new ReceivedItemStructure( mLastID, mUserID, mSmsBody, mSmsNumber, mSenderName, mContactName, mDate ); items.add(item); adapter.update(items); adapter.notifyDataSetChanged(); } public class GetSMSThread extends TimerTask { private Long lastID; private SQLiteDatabase dbHelper; private List<ReceiveListFragment> rows; @Override public void run() { lastID = getLastID(); if (Configuration.getInstance().checkInternetConnection(G.context)) { try { Thread threadTask = new Thread() { @Override public void run() { G.activity.runOnUiThread(new Runnable() { @Override public void run() { try { int countSMS = 0; String smsReceivedSender = ""; String receive_lastID = ""; String r = new JsonService(config_username, config_password, 0, 1, G.F_RECEIVE_SMS).request(); JSONArray data_array = new JSONArray(r); ArrayList<String> items_array = new ArrayList<String>(); JSONObject json_obj = data_array.getJSONObject(0); receive_lastID = json_obj.getString("id_recived_sms"); smsReceivedSender = json_obj.getString("mobile_number"); for (ReceivedItemStructure rf : items) { items_array.add(rf.getmLastID()); } if (items_array.indexOf(receive_lastID) == -1) { countSMS++; addDataToList( json_obj.getString("id_recived_sms"), json_obj.getString("sms_body"), json_obj.getString("sms_number"), json_obj.getString("mobile_number"), json_obj.getString("contact_name"), json_obj.getString("recived_date") ); } } catch (Exception e) { e.printStackTrace(); } } }); } }; threadTask.start(); } catch (Exception e) { e.printStackTrace(); } // END TRY } } } } 无法正常工作。

public class ReceivedAdapter extends ArrayAdapter<ReceivedItemStructure> {

    private ArrayList<ReceivedItemStructure> list;

    public ReceivedAdapter(Context c, ArrayList<ReceivedItemStructure> items) {
        super(c,0,items);
        list = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ReceiveItemView itemView = (ReceiveItemView)convertView;
        if (null == itemView)
            itemView = ReceiveItemView.inflate(parent);

        itemView.setItem(getItem(position));
        return itemView;
    }

    public void update(ArrayList<ReceivedItemStructure> items) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            addAll(items);
        } else {
            for (ReceivedItemStructure item : items) {
                add(item);
            }
        }
    }
}

我的自定义适配器代码是这样的:

setListAdapter(adapter);

items工作正常且我没有任何问题,但在我将新数据添加到<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21"/> notifyDataSetChanged 后,它无法正常工作。< / p>

我的明确内容:

{{1}}

2 个答案:

答案 0 :(得分:1)

它是处理数据集的超类,因为您没有覆盖getCountgetItem

public void update(ArrayList<ReceivedItemStructure> items) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        addAll(items);
    } else {
       for (ReceivedItemStructure item : items) {
             add(item)
        }
    }
}
addAll引入了

Honeycomb,因此您可能想要检查当前正在运行应用的当前支持的sdk

答案 1 :(得分:0)

您正在修改原始项目集合。但是你应该修改适配器的数据。使用ArrayAdapter.add方法添加新项目。