【问题标题】:ListView Adapter with multiple Item layouts not working具有多个项目布局的 ListView 适配器不起作用
【发布时间】:2020-01-26 07:27:54
【问题描述】:

我正在尝试显示一些具有不同布局的文档和图像的 ListView。 它适用于文档,但图像仍未显示。 我使用 .contains 方法来检查项目是文档还是图像。帮我解决这个问题。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = activity.getLayoutInflater();
    String fileName = uriList.get(position).getFileName();

    return viewSetup(position, layoutInflater, fileName);
}

private View viewSetup(final int position, LayoutInflater layoutInflater, String fileName) {
    if (fileName.contains(".png") || fileName.contains(".jpg") || fileName.contains(".jpeg")) {
        View inflate = layoutInflater.inflate(R.layout.main_list_item_img, null, false);
        ImageView imageView = inflate.findViewById(R.id.imgPrev);
        Glide.with(activity).load(uriList.get(position).getDownloadLink()).into(imageView);
        itemSetup(position, fileName, inflate);
        return inflate;

    } else {
        View inflate = layoutInflater.inflate(R.layout.main_list_item_docs, null, false);
        itemSetup(position, fileName, inflate);
        return inflate;

    }
}

private void itemSetup(final int position, String fileName, View inflate) {
    TextView title = inflate.findViewById(R.id.uriTitle);
    TextView desc = inflate.findViewById(R.id.uriDesc);
    ImageView download = inflate.findViewById(R.id.download);
    TextView createdOn = inflate.findViewById(R.id.createdOn);
    title.setText(fileName + "");
    desc.setText(uriList.get(position).getDescription());
    createdOn.setText(uriList.get(position).getSendTime());
    download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            savefile(uriList.get(position).getDownloadLink());
        }
    });
}

【问题讨论】:

  • 尝试调试,如果它跳转到膨胀 main_list_item_img
  • 粘贴任意图片的下载链接。
  • “uriList.get(position).getDownloadLink()”是否返回正确的 url?也请回复@AlexMamo

标签: java android firebase android-studio google-cloud-firestore


【解决方案1】:

我认为您的图片加载有问题。你可以使用毕加索。

  Picasso.with(activity).load(yourUrl).
                        placeholder(R.drawable.image_loader).into(myImage);

【讨论】:

    【解决方案2】:

    你需要使用类型变量,你可以检查我在下面的例子中是怎么做的:-

    public class JobsAdapter extends BaseAdapter {
    
    private Activity context;
    private LinkedList<KeyValuesPair> listItemArrayList;
    private LinkedList<Integer> type;
    private LayoutInflater inflater;
    
    public JobsAdapter(Activity context, LinkedList<KeyValuesPair> objects, LinkedList<Integer> type) {
        this.context = context;
        this.listItemArrayList = objects;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }
    
    @Override
    public int getCount() {
        return type.size();
    }
    
    @Override
    public Object getItem(int position) {
        return position;
    }
    
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public boolean isEnabled(int position) {
        return type.get(position) != 0;
    }
    
    @TargetApi(Build.VERSION_CODES.O)
    public View getView(int position, View convertView, ViewGroup parent) {
        // used new view instead of convertView so the spinner could support multiple views
        View view = null;
        // if type is equals to 0 it will load jobs header else jobs child
        if (type.get(position) == 0) {
            view = inflater.inflate(R.layout.lv_header, null, false);
            TextView header = view.findViewById(R.id.jobsHeading);
            header.setText(listItemArrayList.get(position).getValue());
            if (position == 0) {
                header.setTextSize(16);
                header.setTypeface(context.getResources().getFont(R.font.raleway_regular));
                header.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
            } else {
                header.setTextColor(context.getResources().getColor(R.color.colorBlack));
            }
        } else if (type.get(position) == 1) {
            view = inflater.inflate(R.layout.lv_child, null, false);
            TextView child = view.findViewById(R.id.jobsChild);
            child.setText(listItemArrayList.get(position).getValue());
        }
        return view;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多