Android 滚动显示发送和接收的sms颜色变化的列表视图

Android 滚动显示发送和接收的sms颜色变化的列表视图,android,listview,android-listview,Android,Listview,Android Listview,您好,我正在开发一个android短信应用程序,我想在列表视图上以两种不同的颜色显示发送和接收的短信。如果它发送短信,我正在改变适配器的颜色。如果假设ListView上有10个项目,那么下面的代码可以正常工作 if(type.equalsIgnoreCase("1")) { //received sms } else if(type.equalsIgnoreCase("2")) { //sent sms msg.

您好,我正在开发一个android短信应用程序,我想在
列表视图上以两种不同的颜色显示发送和接收的短信。如果它发送短信,我正在改变适配器的颜色。如果假设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项仍然保留为前一视图颜色。当我来回滚动时,listview中的颜色不断变化。我为
列表视图添加了
android:cacheColorHint=“#000000”
。我不知道我会错在哪里。我如何解决这个问题?请帮忙


谢谢

您必须根据为listitem分配的颜色维护每个listview项的状态

更新-

列表中复选框的相同问题也发生在我身上。它正在自动更改

  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;
}
公共类ListAdapter扩展了BaseAdapter{
私有列表mName;
米肯私人名单;
私有上下文;
私人停车场;
私有数据助手mDataHelper;
私有列表名称;
布尔[]checkBoxState;
private ArrayList itemChecked=新建ArrayList();
公共ListAdapter(上下文mContext、列表名称、列表应用图标){
this.mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
this.mContext=mContext;
this.mName=Name;
这个.mIcon=图标;
mDataHelper=newdatahelper(mContext);
DBappName=newarraylist();
DBappName=mDataHelper.selectName();
/***初始化***/
for(int i=0;i对于(intj=0;j我认为您正在使用自定义布局来查看列表项

所以现在发生的是,当您使用自定义布局时,它将使布局膨胀,并将再次用于下一个项目

因此,自定义适配器的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
 }
这对你有用

(它只是代码逻辑表示,所以检查标准需要由您设置。)


希望有帮助!!

为了更清晰,请粘贴您的
getView
方法好吗?