Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a custom adapter to set values to textview and other components in a listview but on some devices the value to Textview is not set and shows the default value which is used while designing.

In below code the txtAmount is not set and other textview value is set,but this issue is only in some devices

Java
public class ImageCustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] name;
    private final String[] imagepath;
    private final String[] address;
    private final String city[];
    private final String pincode[];
    private final String ISO[];
    private final String NABL[];
    private final String CAP[];
    private final String Service[];
    private final String HomeCollection[];
    private final String Amount[];
    private final String DiscountedAmount[];
    private final String Test[];
    private final String TestAmount[];
    private final double HomeCollectionCharges[];
    private final String FlagSearchFrom;

    customButtonListener customListner;

    public interface customButtonListener {
        public void onButtonClickListner(int position,String value);
    }

    public void setCustomButtonListner(customButtonListener listener) {
        this.customListner = listener;
    }

    public ImageCustomList(Activity context,String[] name,String [] imagepath, String [] address,String [] city,String [] pincode,String [] ISO,String [] NABL,String [] CAP,String [] Service,String []HomeCollection,String [] Amount,String [] DiscountedAmount,String [] Test, String [] TestAmount,double [] HomeCollectionCharges,String FlagSearchFrom) {
        super(context, R.layout.list_row,name);
        this.context = context;
        this.name = name;
        this.imagepath=imagepath;
        this.address = address;
        this.city=city;
        this.pincode=pincode;
        this.ISO=ISO;
        this.NABL=NABL;
        this.CAP=CAP;
        this.Service=Service;
        this.HomeCollection=HomeCollection;
        this.Amount=Amount;
        this.DiscountedAmount=DiscountedAmount;
        this.Test=Test;
        this.TestAmount=TestAmount;
        this.HomeCollectionCharges=HomeCollectionCharges;
        this.FlagSearchFrom=FlagSearchFrom;
    }

    @Override
    public View getView(final int position,View view,ViewGroup viewGroup) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_row, null, true);
        TextView txtName = (TextView) rowView.findViewById(R.id.title);
        //TextView txtAddress = (TextView) rowView.findViewById(R.id.address);
        ImageView imgView =(ImageView)rowView.findViewById(R.id.list_image);

        /*ImageView imgISO =(ImageView)rowView.findViewById(R.id.imgISO);
        ImageView imgNABL =(ImageView)rowView.findViewById(R.id.imgNABL);
        ImageView imgCAP =(ImageView)rowView.findViewById(R.id.imgCAP);
        ImageView imgService =(ImageView)rowView.findViewById(R.id.imgService24);
        ImageView imgHome =(ImageView)rowView.findViewById(R.id.imgHomeCollection);*/

        TextView txtCAP=(TextView)rowView.findViewById(R.id.txtISO);

        TextView txtCity=(TextView)rowView.findViewById(R.id.city);
        //TextView txtPincode=(TextView)rowView.findViewById(R.id.pincode);
        TextView txtAmount=(TextView)rowView.findViewById(R.id.txtAmount);
        TextView txtDiscountedAmount=(TextView)rowView.findViewById(R.id.txtDiscounted);
        TextView txtIndividualTest=(TextView)rowView.findViewById(R.id.txtIndividualTest);
        TextView txtHomeCollection=(TextView)rowView.findViewById(R.id.txtHomeCharges);

        String test_comma_sep=Test[position];
        String amount_comma_sep=TestAmount[position];

        ArrayList<String> lstTest=new ArrayList<>(Arrays.asList(test_comma_sep.split(",")));
        ArrayList<String> lstAmount=new ArrayList<>(Arrays.asList(amount_comma_sep.split(",")));

        String create_indi_test_amt_text="";
        for(int i=0;i<lstTest.size();i++) {
            create_indi_test_amt_text=create_indi_test_amt_text+lstTest.get(i).toString()+" - Rs."+lstAmount.get(i).toString();
            if(i!=lstTest.size()) {
                create_indi_test_amt_text=create_indi_test_amt_text+"\n";
            }
        }

        txtIndividualTest.setText(create_indi_test_amt_text);

        // Loader image - will be shown before loading image
        int loader = R.drawable.ic_logoheart;

        txtName.setText(name[position]);

        ViewHolder holder=new ViewHolder();
        holder.btnBook=(Button)rowView.findViewById(R.id.btnBook);
        holder.btnBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customListner.onButtonClickListner(position,"Book");
            }
        });

        txtCAP.setText(Html.fromHtml(lab_Feature));
        txtCity.setText(city[position]);

        if(DiscountedAmount[position].equals("")) {
            txtAmount.setTextColor(Color.parseColor("#0391D1"));
            txtDiscountedAmount.setText("");
            if(FlagSearchFrom.equals("Test")) {
                if(HomeCollectionCharges[position]==0){
                    txtHomeCollection.setText("Home Collection Charges:FREE");
                    txtAmount.setText("Rs." + Amount[position]);
                }
                else{
                    txtHomeCollection.setText("Home Collection Charges: Rs."+HomeCollectionCharges[position]);
                    txtAmount.setText("Rs." + Amount[position]);
                }
            }
            else {
                txtAmount.setText("Rs." + Amount[position]);
                txtHomeCollection.setVisibility(View.GONE);
            }
        }
        else{

            txtAmount.setPaintFlags(txtAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            txtDiscountedAmount.setText("Rs." + DiscountedAmount[position]);
            if(FlagSearchFrom.equals("Test")) {
                if(HomeCollectionCharges[position]==0){
                    txtHomeCollection.setText("Home Collection Charges:FREE");
                    txtAmount.setText("Rs." + Amount[position]);
                }
                else{
                    txtHomeCollection.setText("Home Collection Charges: Rs."+HomeCollectionCharges[position]);
                    txtAmount.setText("Rs." + Amount[position]);
                    txtDiscountedAmount.setText("Rs." + DiscountedAmount[position]);
                }
            }
            else {
                txtAmount.setText("Rs." + Amount[position]);
                txtHomeCollection.setVisibility(View.GONE);
            }

        }

        try {

            ImageLoader imgLoader = new ImageLoader(getContext().getApplicationContext());

            imgLoader.DisplayImage(imagepath[position], loader, imgView);
        }
        catch (Exception ex){

        }

        return rowView;
    }

    public class ViewHolder {
        Button btnBook;
    }
}


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="wrap_content"
    android:removed="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >



    <!-- Name of Lab-->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_removed"
        android:layout_height="wrap_removed"
        android:layout_alignTop="@+id/logo"
        android:layout_alignParentLeft="true"
        android:textColor="#3D3D3D"
        android:typeface="sans"
        android:textSize="19sp"
        android:textStyle="bold"
        android:text="Title"
        />

    <LinearLayout
        android:id="@+id/linearAmt"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_alignParentLeft="true">
    <!--Amount text -->
    <TextView
        android:id="@+id/txtAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:textSize="16sp"
        android:text="Rs.1234.00"
        android:layout_weight="1"/>

        <TextView
            android:id="@+id/txtDiscounted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0391D1"
            android:textSize="21sp"
            android:paddingLeft="16sp"
            android:layout_below="@+id/title"
            android:text="Rs.1000.00"
            android:layout_weight="1"/>
    </LinearLayout>

    <TextView
        android:id="@+id/txtHomeCharges"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFA500"
        android:textSize="8sp"
        android:layout_below="@+id/linearAmt"
        android:text="Home Collection Charges: Rs.100.00"
        />

    <TextView
        android:id="@+id/txtIndividualTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtHomeCharges"
        android:textColor="#000000"
        android:textSize="8sp"
        android:layout_marginTop="1dip"
        android:layout_toLeftOf="@+id/logo"
        android:layout_alignParentLeft="true"
        android:singleLine="false"
        android:paddingRight="5sp"
        android:text="Just gona \n stand  \n there and ...\n" />

    <!-- Address -->
    <!--<TextView
        android:id="@+id/address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtIndividualTest"
        android:textColor="#7D7D7D"
        android:textSize="18sp"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/logo"
        android:singleLine="false"
        android:layout_weight="1"
        android:ellipsize="none"
        android:maxLines="100"
        android:scrollHorizontally="false"
        android:paddingRight="5sp"
        android:text="Just gona stand there and ..." />-->

    <!--City -->
    <TextView
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7D7D7D"
        android:textSize="16sp"
        android:layout_marginTop="1dip"
        android:layout_below="@+id/txtIndividualTest"
        android:layout_toLeftOf="@+id/logo"
        android:layout_alignParentLeft="true"
        android:text="City"/>

    <!--Pincode -->
    <!--<TextView
        android:id="@+id/pincode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7D7D7D"
        android:textSize="18sp"
        android:layout_marginTop="1dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/address"
        android:layout_toRightOf="@+id/city"
        android:text="Pincode"/>-->

    <!--ISO Ceritified Icon -->
    <!--<ImageView
        android:id="@+id/imgISO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:src="@drawable/tickmark"
        android:layout_toRightOf="@+id/logo"
        android:layout_below="@+id/city"
        />-->

    <!--ISO Ceritified Text -->
    <TextView
        android:id="@+id/txtISO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="16sp"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/city"
        android:layout_toLeftOf="@+id/logo"
        android:layout_alignParentLeft="true"
        android:text="ISO Certified"
        android:maxLines="5"
        android:singleLine="false"/>

    <!--NABLCertified Ceritified Icon -->
    <!--<ImageView
        android:id="@+id/imgNABL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dip"
        android:paddingLeft="15sp"
        android:src="@drawable/tickmark"
        android:layout_toRightOf="@+id/txtISO"
        android:layout_below="@+id/city"
        />-->

    <!--NABLCertified Ceritified Text -->
    <!--<TextView
        android:id="@+id/txtNABL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/city"
        android:layout_toRightOf="@+id/imgNABL"
        android:text="NABL Certified"/>-->

    <!--CAPCertified Ceritified Icon -->
    <!--<ImageView
        android:id="@+id/imgCAP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:paddingTop="10sp"
        android:src="@drawable/tickmark"
        android:layout_toRightOf="@+id/logo"
        android:layout_below="@+id/imgISO"
        />-->

    <!--CAPCertified Ceritified Text -->
    <!--<TextView
        android:id="@+id/txtCAP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/txtISO"
        android:layout_toRightOf="@+id/imgCAP"
        android:text="CAP Certified"/>-->

    <!--Service 24 Hrs Icon -->
    <!--<ImageView
        android:id="@+id/imgService24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:paddingLeft="15sp"
        android:src="@drawable/tickmark"
        android:layout_toRightOf="@+id/txtCAP"
        android:layout_below="@+id/imgNABL"
        />-->

    <!--Service 24 Hrs Text -->
    <!--<TextView
        android:id="@+id/txtService24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/txtNABL"
        android:layout_toRightOf="@+id/imgService24"
        android:text="24 Hours Service"/>-->

    <!--Home Collection Icon -->
    <!--<ImageView
        android:id="@+id/imgHomeCollection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10sp"
        android:src="@drawable/tickmark"
        android:layout_toRightOf="@+id/logo"
        android:layout_below="@+id/imgCAP"
        />-->

    <!--Home Collection Text -->
    <!--<TextView
        android:id="@+id/txtHomeCollection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="3dip"
        android:paddingLeft="2sp"
        android:layout_below="@+id/txtCAP"
        android:layout_toRightOf="@+id/imgHomeCollection"
        android:text="Home Collection Facility"/>-->

    <!--  ListRow Left sied Logo image -->
    <LinearLayout android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:paddingLeft="2dp"
        android:layout_alignParentRight="true"
        >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="108sp"
            android:layout_height="70dip"
            android:scaleType="centerInside"
            android:layout_gravity="center|top"
            android:layout_marginRight="0dp"
            />

    </LinearLayout>

    <Button
        android:id="@+id/btnBook"
        android:layout_width="108sp"
        android:layout_height="wrap_content"
        android:text="BOOK"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:background="#FFA500"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/logo"
        android:layout_marginRight="0dp"
        android:layout_alignParentRight="true"
        />

</RelativeLayout>


What I have tried:

I tried to set a hardcoded value but its wrking fine in all devices
Posted
Comments
David Crow 25-Aug-17 15:10pm    
Have you tried trimming your adapter and XML code down to the bare minimum and still be able to reproduce the problem? As it is, most of what you've shown is unrelated to the problem. My suggestion would be to work with one TextView field, and use string literals when calling setText(). If that works, add another. Etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900