私はAndroidを初めて使用し、リストビューのアイテムの背景に問題があります...うまくいくようです(「プレミアム= 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>