【问题标题】:How to update the dynamic listView values in android如何更新android中的动态listView值
【发布时间】:2013-11-28 12:31:57
【问题描述】:

我正在处理 TCP 套接字,我每秒钟都会收到一些数据,并且需要在 ListView 中显示它,因为我正在使用带有自定义数组适配器的 动态 listView强>.

我的问题是,当我从 TCP 套接字接收到一组新数据时,正在为 listItem (findViewById) 创建一个新对象,因为当我滚动我的列表视图时,它再次移动到顶部当我更新我的列表视图。

当我只考虑一组数据时,我没有发现上述问题。

我的代码:

doAsynchronousTask = new TimerTask() {

        @Override
        public void run() {
            finalizer = new Runnable() {
                public void run() {
                            try {                                   
                                new RetriveStock().execute(); // AsyncTask Executes for every 1 sec.
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                    }
                }
            };
            handler.post(finalizer);
        }
    };

    timer.schedule(doAsynchronousTask, 0, 1000); // execute in every 1000

// 异步任务

    public class RetriveStock extends AsyncTask<Void, Void, ArrayList<User>> {

    @Override
    protected ArrayList<User> doInBackground(Void... params) {
        message += client.clientReceive(1); // Receive data from socket and put into message (string global variable).
        printJson(); // Here I receive data from JSON and put into object
        adb = new RSSListAdaptor(DefaultMarketWatch.this,
                R.layout.rssitemview, list); // "list" is a global listAdapter.
        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    protected void onPostExecute(ArrayList<User> result) {

        lv.setAdapter(adb);
        adb.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}

// printJson()

    public void printJson() {
    String str = "";

    /* SOME CODE HERE
    FORMATING "message" (STRING VARIABLE) TO A PERFECT JSONARRAY FORMATE, AND PUT INTO VARIABLE "str" */
    str = "[" + str + "]";

    try {
        JSONArray jsonArray = new JSONArray(str);

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
        User obj = new User();

        // MY CODE, EITHER ADDING NEW OBJECT OR UPDATING THE EXISTING USER OBJECT.

        list.add(obj);

    } catch (JSONException e1) {
        e1.printStackTrace();
    }
}

// 我的自定义 ArrayAdapter.

public class RSSListAdaptor extends ArrayAdapter<User> {
    private List<User> objects = null;

    public RSSListAdaptor(Context context, int textviewid,
            List<User> objects) {
        super(context, textviewid, objects);
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return ((null != objects) ? objects.size() : 0);
    }

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


    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.rssitemview, null);

        }

        try {
            User u = objects.get(position);
            if (null != u) {

                TextView title_list = (TextView) view.findViewById(R.id.symbol);
                TextView ltp_list = (TextView) view.findViewById(R.id.ltp);
                TextView high_list = (TextView) view.findViewById(R.id.high);
                TextView low_list = (TextView) view.findViewById(R.id.low);
                TextView persend_list = (TextView) view.findViewById(R.id.persent);

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title_list
                        .getLayoutParams();

            params.setMargins(0, 0, 25, 0);
            title_list.setText("TITLE");
            ltp_list.setText("LTP");
            high_list.setText("HIGH");
            low_list.setText("LOW");
            persend_list.setText("PERSENTAGE");

            }
        } catch (IllegalStateException is) {
            is.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

}

【问题讨论】:

    标签: android listview android-listview android-asynctask


    【解决方案1】:

    试试这个:

    // save index and top position
    int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0);
    int top = (v == null) ? 0 : v.getTop();
    
    // update listview with new data
    
    // restore
    mList.setSelectionFromTop(index, top);
    

    或者

    // Save ListView state
    Parcelable state = listView.onSaveInstanceState();
    
    // Set new items
    listView.setAdapter(adapter);
    
    // Restore previous state (including selected item index and scroll position)
    listView.onRestoreInstanceState(state);
    

    【讨论】:

      【解决方案2】:

      不要在doInBackground 中重新创建适配器,也不要在onPostExecute 中重置它

      宁可在你的构造函数中或其他合理的地方创建一个空的适配器,当数据进来时,将该数据附加到你的适配器然后调用notifyDataSetChanged

      【讨论】:

        猜你喜欢
        • 2010-12-16
        • 1970-01-01
        • 2011-08-16
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-01
        相关资源
        最近更新 更多