問題 :
リストビューにレイアウトを表示することになっている単純なアプリケーション。レイアウトには次のようなデータが含まれています。
ImageView 1--- Text View 1/ (下) Text View 2--- Image View 2
問題は、Galaxy S4 や Grand Duos などの一部のデバイスで Image View 2 が表示されないことです。
働く :
動作していません:
コード :
プロジェクト構造:
MainActivity コード:
package com.example.screenfix;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("App_Name", "Test Data");
map.put("App_Desc", "Test Description");
map.put("Price", "free");
map.put("image_url","importicon");
songsList.add(map);
BinderData bindingData = new BinderData(this,songsList);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(bindingData);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
アダプターコード:
package com.example.screenfix;
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 arg0;
}
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.rowdata, null);
holder = new ViewHolder();
holder.tvCity = (TextView)vi.findViewById(R.id.textView1); // city name
holder.tvWeather = (TextView)vi.findViewById(R.id.textView2); // city weather overview
holder.tvTemperature = (ImageView)vi.findViewById(R.id.imageView2); // city temperature
holder.tvWeatherImage =(ImageView)vi.findViewById(R.id.imageView1); // 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;
}
}
MainActivity xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
ListRowData:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_weight="0.20"
android:scaleType="fitStart"
android:src="@drawable/importicon" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.70"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 2" />
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.10"
android:src="@drawable/free" />
</LinearLayout>
どんな助けでも大歓迎です...