ほとんどのデバイスで適切に機能するカスタム リストの実装がありますが、galaxy s4 や grand duos などの少数のデバイスでのみ、子要素の最後の画像が表示されません。
コードは以下の通りです..
メイン リスト ビュー:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundimage"
android:keepScreenOn="true"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
子要素コード:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/App_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/list_image"
android:layout_toRightOf="@+id/list_image"
android:scrollHorizontally="false"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:textSize="12dip"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/App_Desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/App_Name"
android:layout_below="@+id/App_Name"
android:scrollHorizontally="false"
android:text="Just gona stand there and ..."
android:textColor="#343434"
android:textSize="10dip"
android:typeface="monospace" />
</LinearLayout>
<ImageView
android:id="@+id/Price"
android:layout_width="20dip"
android:layout_height="60dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/buy_gas" />
</LinearLayout>
</RelativeLayout>
アダプターコード
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class BinderData extends BaseAdapter {
// XML node keys
static final String KEY_TAG = "weatherdata"; // parent node
static final String KEY_ID = "id";
static final String KEY_CITY = "city";
static final String KEY_TEMP_C = "tempc";
static final String KEY_TEMP_F = "tempf";
static final String KEY_CONDN = "condition";
static final String KEY_SPEED = "windspeed";
static final String KEY_ICON = "icon";
LayoutInflater inflater;
ImageView thumb_image;
List<HashMap<String,String>> weatherDataCollection;
ViewHolder holder;
public BinderData() {
// TODO Auto-generated constructor stub
}
public BinderData(Activity act, List<HashMap<String,String>> map) {
this.weatherDataCollection = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
// return idlist.size();
return weatherDataCollection.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.tvCity = (TextView)vi.findViewById(R.id.App_Name); // city name
holder.tvWeather = (TextView)vi.findViewById(R.id.App_Desc); // city weather overview
holder.tvTemperature = (ImageView)vi.findViewById(R.id.Price); // city temperature
holder.tvWeatherImage =(ImageView)vi.findViewById(R.id.list_image); // thumb image
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
// Setting all values in listview
holder.tvCity.setText(weatherDataCollection.get(position).get("App_Name"));
holder.tvWeather.setText(weatherDataCollection.get(position).get("App_Desc"));
String uri = "";
if(weatherDataCollection.get(position).get("Price").equalsIgnoreCase("pro"))
{
uri = "drawable/label";
}else{
uri = "drawable/free";
}
int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
Drawable image = vi.getContext().getResources().getDrawable(imageResource);
holder.tvTemperature.setImageDrawable(image);
//holder.tvTemperature.setText(weatherDataCollection.get(position).get("Price"));
//Setting an image
String uri2 = "drawable/"+ weatherDataCollection.get(position).get("image_url");
int imageResource2 = vi.getContext().getApplicationContext().getResources().getIdentifier(uri2, null, vi.getContext().getApplicationContext().getPackageName());
Drawable image2 = vi.getContext().getResources().getDrawable(imageResource2);
holder.tvWeatherImage.setImageDrawable(image2);
return vi;
}
/*
*
* */
static class ViewHolder{
TextView tvCity;
ImageView tvTemperature;
TextView tvWeather;
ImageView tvWeatherImage;
}
}
リスト ビューの実装:
BinderData bindingData = new BinderData(this,songsList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(bindingData);
これについて私たちを助けてください..