1

我正在动态地将项目添加到 ListView 中,并且我遇到了新条目仅在我触摸手机屏幕时才会显示的问题。

我正在使用此代码添加一个新项目:

public TimerTask tt = new TimerTask() {
    @Override
    public void run() {
        LoadChatData bdl = new LoadChatData(new OnTaskComplete() {
            @Override
            public void onTaskCompleted(ArrayList<ChatListItem> item, int id) {
                currid = id;
                for (ChatListItem i : item) {
                    dataarr.add(i);
                }
                chatList.notifyDataSetChanged();
            }
        });
        bdl.execute(XXX);
    }
};

chatList 是适配器,dataarr 是适配器持有的数据。条目被添加,但只有当我触摸手机屏幕时才会显示它们。

我的自定义适配器如下所示:

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ChatList extends BaseAdapter {

    java.util.List<ChatListItem> data = null;
    Context context;
    int layoutResourceId;
    public String me = null;

    public void setId(String aid){
        this.me = aid;
    }

    public ChatList(Context context, int layoutResourceId, ArrayList<ChatListItem> data) {
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ChatHolder holder = null;
        ChatListItem entry = data.get(position);

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(layoutResourceId, parent, false);


            holder = new ChatHolder();
            holder.txtText = (TextView) row.findViewById(R.id.txtText);
            holder.txtUser = (TextView) row.findViewById(R.id.txtUser);
            holder.txtVersion = (TextView) row.findViewById(R.id.txtVersion);
            holder.txtZeit = (TextView) row.findViewById(R.id.txtZeit);
            holder.background = (RelativeLayout) row.findViewById(R.id.backgroundlayout);

            row.setTag(holder);

        } else {
            holder = (ChatHolder) row.getTag();
        }

        int status = Integer.parseInt(entry.status);

        /*
         * android:paddingBottom="5dp" android:paddingLeft="5dp"
         * android:paddingRight="15dp" android:paddingTop="5dp"
         */

        if (entry.uid.equals(me)) {
            holder.background.setPadding(20, 5, 30, 5);
            holder.background.setBackgroundResource(R.drawable.chatitem_me);
        } else {
            holder.background.setPadding(30, 5, 20, 5);
            holder.background.setBackgroundResource(R.drawable.chatitem);
        }

        holder.txtUser.setTextColor(0xFF000000);
        holder.txtText.setTextColor(0xFF000000);

        Log.d("Info", "status is : " + status);
        if (status == 1) {
            holder.txtUser.setTextColor(0xFF005CE6);
        } else if (status == 2) {
            holder.txtUser.setTextColor(0xFFD67600);
        } else if (status == 3) {
            holder.txtUser.setTextColor(0xFF000000);
        } else if (status == 4) {
            holder.txtUser.setTextColor(0xFFDB0000);
            holder.txtText.setTextColor(0xFF660000);
        } else if (status == 5) {
            holder.txtUser.setTextColor(0xFF009933);
            holder.txtText.setTextColor(0xFF003300);
        }

        holder.txtText.setText(entry.text);
        holder.txtUser.setText(entry.user);
        holder.txtVersion.setText(entry.v);
        holder.txtZeit.setText(entry.time);

        return row;
    }

    static class ChatHolder {
        RelativeLayout background;
        TextView txtText;
        TextView txtUser;
        TextView txtVersion;
        TextView txtZeit;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public ChatListItem getItem(int arg0) {
        return data.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
}
4

2 回答 2

0

计时器的愚蠢之处。我不得不在 UI 线程中更新我的 UI。

于 2013-02-18T12:52:43.690 回答
0

您还记得使用Timer.schedule()方法启动计时器吗?

于 2013-02-16T21:42:44.833 回答