我是 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>