android studio java中的Listview

pokxtpni  于 2023-08-07  发布在  Android
关注(0)|答案(1)|浏览(119)

我试图实现一个列表视图,但我有一个错误,在适配器类上

View view = LayoutInflater.from(context).inflate(R.layout.agences_items,parent,false);

字符串
当编译时,错误显示错误:我真的不明白这个问题。
我已经确保view.layoutInflater的库,但错误仍然存在

`
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class MyAdapter extends ArrayAdapter {

    List<String> listTitle;
    List<Integer> imagelist;
    Context context;

    public MyAdapter(@NonNull Context context, List<String> title,List<Integer> image) {
        super(context, R.layout.agences_items,title);

        this.listTitle = title;
        this.imagelist = image;
        this.context = context;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return super.getView(position, convertView, parent);
        View view = LayoutInflater.from(context).inflate(R.layout.agences_items,parent,false);
        ImageView imageView = view.findViewById(R.id.imgview);
        TextView textView = view.findViewById(R.id.txtview);

        textView.setText(listTitle.get(position));
        imageView.setImageResource(imagelist.get(position));

        return view;

    }
}


` enter image description here

mgdq6dx1

mgdq6dx11#

getView(...)方法中,在执行设置List View的代码之前返回一个值。删除它,您的列表视图应该会像预期的那样膨胀。

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //remove the line below
        //return super.getView(position, convertView, parent);
        View view = LayoutInflater.from(context).inflate(R.layout.agences_items,parent,false);
        ImageView imageView = view.findViewById(R.id.imgview);
        TextView textView = view.findViewById(R.id.txtview);

        textView.setText(listTitle.get(position));
        imageView.setImageResource(imagelist.get(position));

        return view;

    }

字符串
在大多数编程语言中,如果编译器遇到return语句,则该语句下面的其余代码通常不会到达,因为值被返回到调用函数并且代码块被退出。

相关问题