我正在动态地向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;
}
}