嗨,我正在开发一个安卓短信应用程序,我想在ListView上显示发送和接收到的两种不同颜色的短信。如果它发送短信,我在适配器中改变颜色。如果假设ListView上有10个条目,那么下面的代码可以正常工作。
if(type.equalsIgnoreCase("1"))
{
//received sms
}
else if(type.equalsIgnoreCase("2"))
{
//sent sms
msg.setBackgroundColor(Color.parseColor("#D5F7C3"));
msgdt.setBackgroundColor(Color.parseColor("#BDF8C7"));
}但是在滚动它的时候,第11项仍然和以前的视图颜色一样。当我来回滚动时,列表视图中的颜色一直在变化。我为android:cacheColorHint="#000000"添加了ListView。不知道我哪里出了问题。我该怎么解决这个问题?请帮帮忙。
谢谢!
发布于 2013-10-07 11:25:35
我认为您在列表项中使用自定义布局来查看视图。
所以现在发生的事情是,当您使用自定义布局时,它会膨胀布局,并将再次用于下一个项目。
因此,在自定义适配器的getview()中执行检查条件是类似于此。
if(type.equalsIgnoreCase("1"))
{
//received sms
msg.setBackgroundColor(Color.parseColor("#D5F7C3"));
msgdt.setBackgroundColor(Color.parseColor("#BDF8C7"));
//Set colors for Recieved SMS.
}
else if(type.equalsIgnoreCase("2"))
{
//sent sms
msg.setBackgroundColor(Color.parseColor("#D5F7C3"));
msgdt.setBackgroundColor(Color.parseColor("#BDF8C7"));
//Set Colors for Sent SMS
}它会对你有用的。
(它只是代码逻辑表示,所以检查条件需要由您来设置。)
希望能帮上忙!
发布于 2013-10-07 11:18:47
您必须根据此为listitem分配颜色来维护每个listview项的状态。
更新-
同样的问题也发生在我身上,因为list.it中的复选框正在自动更改。
public class ListAdapter extends BaseAdapter{
private List<String> mName;
private List<Drawable> mIcon;
private Context mContext;
private LayoutInflater mInflater;
private DataHelper mDataHelper;
private List<String> DBappName;
boolean[] checkBoxState;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
public ListAdapter(Context mContext, List<String> Name, List<Drawable> appIcon, ) {
this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mContext=mContext;
this.mName=Name;
this.mIcon=Icon;
mDataHelper=new DataHelper(mContext);
DBappName=new ArrayList<String>();
DBappName=mDataHelper.selectName();
/***Initialization***/
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
for (int i = 0; i < DBappName.size(); i++) {
for(int j=0;j<mName.size();j++){
if(DBappName.get(i).equals(mName.get(j))){
itemChecked.add(j, true);
}
}
}
}
@Override
public int getCount() {
return mName.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_menu, null);
mHolder = new ViewHolder();
mHolder.mText=(TextView) convertView.findViewById(R.id.Name);
mHolder.mImage=(ImageView) convertView.findViewById(R.id.Icon);
mHolder.mCheckBoxLock=(CheckBox) convertView.findViewById(R.id.mCheckbox);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.mText.setText(mName.get(position));
mHolder.mImage.setImageDrawable(mIcon.get(position));
mHolder.mCheckBoxLock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
itemChecked.set(position, true);
mDataHelper.insert(mName.get(position));
}else{
itemChecked.set(position, false);
mDataHelper.delete(mName.get(position));
}
}
});
mHolder.mCheckBoxLock.setChecked(itemChecked.get(position)); // this will Check or Uncheck the
return convertView;
}
private class ViewHolder {
private TextView mText;
private ImageView mImage;
private CheckBox mCheckBoxLock;
}}
https://stackoverflow.com/questions/19223372
复制相似问题