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

标签 java android listview android-listview

我创建了一个自定义 ListView ,左侧有一个图像,右侧有两行文本。它工作正常,我可以显示默认图像。对于文本,我设置了它,因此每个列表项都是不同的,并且使用适配器进行更改,但是我无法让图像执行相同的操作。我在下面发布了一些代码 fragment 来展示已经存在的内容。这可能是一些愚蠢的事情,但我对 Android 开发还很陌生,所以我还不够熟悉,无法意识到出了什么问题。我在 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;
    }
}

结果.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;
       }

最佳答案

在结果类中,您必须具有填充 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;


 }
}

关于java - 使用 ArrayAdapter 通过自定义适配器将动态图像添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23271855/

相关文章:

java - 无法在 Eclipse 上打开声明(未找到源)- HttpServlet.class

java - Websphere Application Server 中所需的 Websphere MQ 类

java - Maven 选择了错误的资源路径

android - 在状态栏中不显示图标的情况下打开/关闭 GPS

java - 在 Android 应用中仅显示截止日期为今天的待办事项

android - 带标题的 ListView

android - 更新 getCheckedItemPositions()

java - SIM小程序开发

android - 如何在 Android Usb 主机中获取主机发送命令后 USB 设备响应的内容?

java - RxJava2 中的 flatMapPublisher 是什么?