android listview设置一些项目的背景(滚动时的问题)

时间:2013-07-11 13:43:01

标签: android listview

我是Android的新用户,我的列表视图中的项目背景有问题...它似乎有用(带有“premium = 1”的项目应该有黄色背景),但是当我向下滚动时每次其他项目都有黄色背景颜色,然后备份?!

我找不到自己的错误:(

Pois.java(重要部分)

protected void ausgeben()
{   
    ausgabestring = ausgabestring.trim();
    ausgabestring = ausgabestring.substring(1);
    rowItems = new ArrayList<PoiRowItem>(); 
    try{            
        jsonArray = new JSONArray(ausgabestring);
        Log.i("ausgabe", "teilsuccess");
        for(int i=0; i < jsonArray.length(); i++)
        {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            PoiRowItem item = new PoiRowItem(jsonObj.getString("POI_NAME"), jsonObj.getString("POI_ID"), jsonObj.getString("POI_PREMIUM"));
            rowItems.add(item);
        }
    }
    catch(JSONException e)
    {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    listView = (ListView) findViewById(R.id.list);
    PoiListViewAdapter adapter = new PoiListViewAdapter(this, R.layout.poi_list_item, rowItems);
    listView.setAdapter(adapter);
    //listView.setOnItemClickListener(this);   
}

PoiListViewAdapter.java(我觉得我的问题在哪里)

import java.util.List;
import at.visualstudioteschl.dguide.PoiRowItem;
import at.visualstudioteschl.dguide.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class PoiListViewAdapter extends ArrayAdapter<PoiRowItem> {

Context context;

public PoiListViewAdapter(Context context, int resourceId, List<PoiRowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

private class ViewHolder {
    TextView txtTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    PoiRowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.poi_list_item, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();
        holder.txtTitle.setText(rowItem.getTitle());

        // TODO HIER GIBTS NOCH NEN FEHLER... ES WERDEN BEIM SCROLLEN NACH KURZER ZEIT ALLE GELB
        if (rowItem.getPremium().contains("1")){
            holder.txtTitle.setBackgroundColor(Color.YELLOW);
        }


    return convertView;
}
}

PoiRowItem.java

public class PoiRowItem {
private String title;
private String kat_id;
private String premium;

public PoiRowItem(String title, String kat_id, String premium) {
    this.title = title;
    this.kat_id = kat_id;
    this.premium = premium;
}

public String getID() {
    return kat_id;
}
public void setID(String id) {
    this.kat_id = id;
}

public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
@Override
public String toString() {
    return kat_id;
}    

public String getPremium() {
    return premium;
}
public void setPremium(String premium) {
    this.premium = premium;
}
}

*的 poi_list_item.xml *

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"     
    android:gravity="left"
    android:padding="5dp"
    android:textSize="20sp"
    android:singleLine="true"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

视图被重复使用..所以当你不想要黄色时需要设置原始颜色。

// TODO HIER GIBTS NOCH NEN FEHLER... ES WERDEN BEIM SCROLLEN NACH KURZER ZEIT ALLE GELB
if (rowItem.getPremium().contains("1")){
    holder.txtTitle.setBackgroundColor(Color.YELLOW);
}
else{
    holder.txtTitle.setBackgroundColor(Color.WHITE); // the original color here
}

答案 1 :(得分:2)

滚动时会重复使用您的列表项,因此在非溢价时需要强制使用正常颜色,例如:

    if (rowItem.getPremium().contains("1")){
        holder.txtTitle.setBackgroundColor(Color.YELLOW);
    }
    else {
        holder.txtTitle.setBackgroundColor(Color.WHITE);
    }