listview不会更新

时间:2013-11-21 10:09:39

标签: android android-listview

我的情况很糟糕,因为我的列表视图只更新了一次 idia正在关注,我想从网页下载帖子,这些帖子位于第3页和第5页,并在listview上显示它们。但是,列表视图仅显示第3页的帖子。虽然日志显示所有poast都已下载并存储在addList中。

所以问题 - objects from second itteration are not shown in listview。请帮我解决这个问题。

public class MyAddActivateActivity extends ErrorActivity implements
        AddActivateInterface {
    // All static variables

    // XML node keys
    View footer;
    public static final String KEY_ID = "not_id";
    public static final String KEY_TITLE = "not_title";
    Context context;
    public static final String KEY_PHOTO = "not_photo";
    public final static String KEY_PRICE = "not_price";
    public final static String KEY_DATE = "not_date";
    public final static String KEY_DATE_TILL = "not_date_till";
    private int not_source = 3;
    JSONObject test = null;
    ListView list;
    MyAddActivateAdapter adapter;
    ArrayList<HashMap<String, String>> addList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;
        setContentView(R.layout.aadd_my_add_list_to_activate);
        footer = getLayoutInflater().inflate(R.layout.loading_view, null);
        addList = new ArrayList<HashMap<String, String>>();
        ImageView back_button = (ImageView) findViewById(R.id.imageView3);
        back_button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(),
                        ChooserActivity.class);
                startActivity(i);
                finish();
            }
        });
        Intent intent = this.getIntent();
//      if (intent != null) {
//          not_source = intent.getExtras().getInt("not_source");
//      }

        GAdds g = new GAdds();
        g.execute(1, not_source);

        list = (ListView) findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter = new MyAddActivateAdapter(this, addList);
        list.addFooterView(footer);
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String add_id = ((TextView) view
                        .findViewById(R.id.tv_my_add_id)).getText().toString();
                add_id = add_id.substring(4);
                Log.d("ADD_ID", add_id);
                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getApplicationContext(),
                        AddToCheckActivity.class);
                // sending data to new activity
                UILApplication.advert.setId(add_id);
                startActivity(i);
                finish();

            }
        });

    }

    private void emptyAlertSender() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Ничего не найдено");
        alertDialog.setMessage("У вас нет неактивных объявлений.");
        alertDialog.setButton("на главную",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getApplicationContext(),
                                ChooserActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
    }

    class GAdds extends AsyncTask<Integer, Void, JSONObject> {

        @Override
        protected JSONObject doInBackground(Integer... params) {
            // addList.clear();
            Log.d("backgraund", "backgraund");
            UserFunctions u = new UserFunctions();
            return u.getAdds(params[0] + "", params[1] + "");
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            Log.d("postexecute", "postexecute");
            footer.setVisibility(View.GONE);
            JSONArray tArr = null;
            if (result != null) {

                try {
                    tArr = result.getJSONArray("notices");

                    for (int i = 0; i < tArr.length(); i++) {

                        JSONObject a = null;
                        HashMap<String, String> map = new HashMap<String, String>();

                        a = (JSONObject) tArr.get(i);
                        if (a.getInt("not_status") == 0) {
                            int premium = a.getInt("not_premium");
                            int up = a.getInt("not_up");
                            if (premium == 1 && up == 1) {
                                map.put("status", R.drawable.vip + "");
                            } else if (premium == 1) {
                                map.put("status", R.drawable.prem + "");
                            } else if (premium != 1 && up == 1) {
                                map.put("status", R.drawable.up + "");
                            } else {
                                map.put("status", 0 + "");
                            }

                            map.put(KEY_ID, "ID: " + a.getString(KEY_ID));
                            map.put(KEY_TITLE, a.getString(KEY_TITLE));
                            map.put(KEY_PRICE,
                                    "Цена: " + a.getString(KEY_PRICE) + " грн.");
                            map.put(KEY_PHOTO, a.getString(KEY_PHOTO));
                            map.put(KEY_DATE,
                                    "Создано: " + a.getString(KEY_DATE));
                            map.put(KEY_DATE_TILL,
                                    "Действительно до: "
                                            + a.getString(KEY_DATE_TILL));
                            map.put("check", "false");
                            Log.d("MyAddList", "map was populated");

                        }

                        if (map.size() > 0)

                        {
                            addList.add(map);
                            Log.d("MyAddList", "addlist was populated");

                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                if (UILApplication.login != 2) {
                    UILApplication.login = 3;

                }

                onCreateDialog(LOST_CONNECTION).show();
            }
            runOnUiThread(new Runnable() {
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
            if (not_source == 3) {
                not_source = 5;
                GAdds task = new GAdds();
                task.execute(1, 5);

            }
            if (not_source == 5) {
                Log.d("ID", m.get(KEY_ID));

                if (addList.size() == 0) {
                    emptyAlertSender();
                }
            }

        }

    }

    @SuppressWarnings("unchecked")
    public void addAct(View v) {
        AddActivate aAct = new AddActivate(this);
        aAct.execute(addList);

    }

    @Override
    public void onAddActivate(JSONObject result) {
        if (result != null) {
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    }

}

适配器

public class MyAddActivateAdapter extends BaseAdapter {
    private List<Row> rows;
    private ArrayList<HashMap<String, String>> data;
    private Activity activity;

    public MyAddActivateAdapter(Activity activity,
            ArrayList<HashMap<String, String>> data) {
        Log.d("mediaadapter", "listcreation: " + data.size());
        rows = new ArrayList<Row>();// member variable
        this.data = data;
        this.activity = activity;

    }

    @Override
    public int getViewTypeCount() {
        return RowType.values().length;
    }

    @Override
    public void notifyDataSetChanged() {
        rows.clear();
        for (HashMap<String, String> addvert : data) {

            rows.add(new MyAddActivateRow((LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE), addvert));
            Log.d("MyActivateAdapter", "update " + data.size());
        }
    }

    @Override
    public int getItemViewType(int position) {
        return rows.get(position).getViewType();
    }

    public int getCount() {
        return rows.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return rows.get(position).getView(convertView);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题是你没有调用notifyDataSetChanged()的超类,也许试试

@Override
public void notifyDataSetChanged() {
    rows.clear();
    for (HashMap<String, String> addvert : data) {

        rows.add(new MyAddActivateRow((LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE), addvert));
        Log.d("MyActivateAdapter", "update " + data.size());
    }
    super.notifyDataSetChanged();
}