简体   繁体   中英

android listview set background of some items (problems when scrolling)

I´m new to Android and i´ve a problem with my items-background in my listview... it seems to work (items with "premium = 1" should have a yellow background), but when i scroll down and then back up everytime other items have the yellow background-color?!

And i can´t find my mistake :(

Pois.java (the important part)

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 (Where i think is my problem)

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>

The views are re-used.. So you need to set the original color back when you don't want the yellow color.

// 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
}

Your list items are reused when scrolling, so you need to force the normal color in case of non-premium, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM