使用ArrayAdapter使用自定义适配器将动态图像添加到ListView

时间:2014-04-24 14:28:53

标签: java android listview android-listview

我创建了一个自定义列表视图,左侧是图像,右侧是两行文本。它工作正常,我可以有一个默认的图像显示。对于我设置的文本,所以每个列表项是不同的,它使用适配器更改,但我不能让图像做同样的事情。我在下方发布了一些代码段,以显示已经存在的内容。它可能是一些愚蠢的东西,但我对Android Dev来说还是一个新手,所以我不太熟悉,无法意识到出了什么问题。我在setImageResource行收到错误。我认为将图像放入阵列列表时我会这么做,但我不确定如何绕过它。

我已经查看了很多stackoverflow问题和解决方案,他们通常会解决我遇到的任何问题,但我无法让这个问题解决。

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 个答案:

答案 0 :(得分:1)

在结果类中,您必须具有填充mainactivity的int属性

 Results kf = new Results();

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

     results.add(kf);

并且结果类必须更改为

 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;


 }
}

答案 1 :(得分:0)

在setImageResource方法中需要int参数,而不是ImageView。改变方法 public ImageView getPlaceholderleft() { return placeholderleft; }

返回R.drawable.your_image