0

I've created a custom listview with an image on the left and two lines of text on the right. It works fine and I can have a default image display. For the text I have it set so each list item is different and it changes using an adapter, however I can't get the image to do the same. I've posted some code snippets below to show what's already there. It's probably something stupid but I'm quite new to Android Dev so I'm not familiar enough to realise what's going wrong. I'm getting an error at the setImageResource line. I think it's to do with trying to put an image into an Array List but I'm not sure how to get around it.

I've looked through a lot of stackoverflow questions and solutions already, they usually solve any issue I've had but I just cannot get this one to work.

row_items_list.xml

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
    android:paddingBottom="10dip"
    android:paddingLeft="10dip"
    android:paddingTop="10dip" >


<ImageView
    android:id="@+id/placeholderleft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="6dp"
    android:scaleType="fitCenter"  
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
 >
</ImageView>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#9370DB"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/authorw1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="12sp"
        android:textStyle="italic"/>
 </LinearLayout>

</LinearLayout>

CustomBaseAdapter.java

package com.example.tildathemes;

import java.util.ArrayList;

import android.content.Context;
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 MyCustomBaseAdapter extends BaseAdapter {
    private static ArrayList<KeyFindings> searchArrayList;
     private LayoutInflater mInflater;


    public MyCustomBaseAdapter(Context context, ArrayList<KeyFindings> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return searchArrayList.size();
    }

    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row_view, null);
            holder = new ViewHolder();
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.txtAuthor = (TextView) convertView
                    .findViewById(R.id.author); 
            holder.picPlaceholderleft = (ImageView) convertView.findViewById(R.id.placeholderleft);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.txtTitle.setText(searchArrayList.get(position).getTitle());
        holder.txtAuthor.setText(searchArrayList.get(position)
                .getAuthor());
        holder.picPlaceholderleft.setImageResource(searchArrayList.get(position)
                .getPlaceholderleft());

        return convertView;
    }

    static class ViewHolder {
        TextView txtTitle;
        TextView txtAuthor;
        ImageView picPlaceholderleft;
    }
}

Results.java

public class Results{
     private String title = "";
     private String author = "";
     private ImageView placeholderleft;

     public void setTitle(String Title) {
      this.title = Title;
     }

     public String getTitle() {
      return title;
     }

     public void setAuthor(String Author) {
      this.author = Author;
     }

     public String getAuthor() {
      return author;
     }

     public void setPlaceholderleft(ImageView Placeholderleft) {
         this.placeholderleft = Placeholderleft;
     }

     public ImageView getPlaceholderleft() {
      return placeholderleft;


     }
}

MainActivity.java

private ArrayList<Results> GetResults(){
        ArrayList<Results> results = new ArrayList<Results>();

        Results kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");

        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Authors");
        **THIS IS WHERE I WANT TO INCLUDE IMAGE**
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Authors");
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");
        results.add(kf);

        kf = new Results();
        kf.setTitle("Title");
        kf.setAuthor("Author");
        results.add(kf);


        return results;
       }

2 Answers 2

1

in the result class you must have int property that filled in mainactivity

 Results kf = new Results();

     kf.setTitle("Title"); 
     kf.setAuthor("Author");
     kf.setPlaceholderleft(R.drawable.yourpic);

     results.add(kf);

and result class must change to

 public class Results{ 
 private String title = "";
 private String author = "";
 private int  placeholderleft;

 public void setPlaceholderleft(int Placeholderleft) {
     this.placeholderleft = Placeholderleft;
 }

 public int getPlaceholderleft() {
  return placeholderleft;


 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In setImageResource method need int parameter, not ImageView. Change method public ImageView getPlaceholderleft() { return placeholderleft; }

return R.drawable.your_image

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.