0

Issue :

Simple application supposed to show a layout in a listview, Layout contains data like:

ImageView 1--- Text View 1/ (below) Text View 2--- Image View 2

Problem is Image View 2 Not Visible in some devices like Galaxy S4 and Grand Duos

Working :

enter image description here

Not Working :

enter image description here

Code :

Project Struct :

enter image description here

MainActivity Code :

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;
    }

}

AdapterCode :

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>

Any Help is appreciated...

1
  • put that icon into Drawable-hdpi and try.... Aug 23, 2013 at 8:45

2 Answers 2

0

apply android:layout_width="match_parent"and change android:layout_weight=""(shown below) to both ImageView and inner LinearLayout in rowdata.xml

here is code

<?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="match_parent"
      android:layout_height="44dp"
      android:layout_weight="2"
      android:scaleType="fitStart"
      android:src="@drawable/ic_launcher" />

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      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="match_parent"
     android:layout_height="50dp"
     android:layout_weight="2"
     android:src="@drawable/ic_launcher" />

</LinearLayout>
0

SOLVED :

The problem was with the right most icon, It was having very high resolution just reduced the resolution and replaced the Files and it worked.

Thanks every one for their time..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.