我有一個Wiered問題我有一個自定義列表實現,它在大多數設備上都能正常工作,但是隻有少數設備,如galaxy s4或grand duos,最後一個子元素的圖像不可見。Android開發 - 自定義列表視圖不顯示所有兒童組件
代碼如下..
主列表視圖:
<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);
請幫助我們這個..
請你加你的ListView類和它的適配器類 – Shrikant
Addeed ..請看看 – sunil