0

我正在研究 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;
    }

}
4

2 回答 2

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);
于 2013-11-15T07:43:34.013 回答
1

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

而是在您的构造函数或合理的地方创建一个空适配器,当数据进入时,将该数据附加到您的适配器然后调用notifyDataSetChanged

于 2013-11-15T07:39:56.390 回答