Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView在正常工作之前会返回顶部几秒钟_Android_Image_List_Listview_Imageview - Fatal编程技术网

Android ListView在正常工作之前会返回顶部几秒钟

Android ListView在正常工作之前会返回顶部几秒钟,android,image,list,listview,imageview,Android,Image,List,Listview,Imageview,我正在尝试制作一个包含国旗和姓名的ListView应用程序。这是为了用户可以点击他们,并显示他们以前拍摄的国家的图像。然而,当listview加载时,如果我尝试滚动,它会出现大约3秒钟的故障,并将我返回顶部。这是密码 public class CountriesListAdapter extends ArrayAdapter { private int resource; private LayoutInflater inflater; private List<C

我正在尝试制作一个包含国旗和姓名的ListView应用程序。这是为了用户可以点击他们,并显示他们以前拍摄的国家的图像。然而,当listview加载时,如果我尝试滚动,它会出现大约3秒钟的故障,并将我返回顶部。这是密码

public class CountriesListAdapter extends ArrayAdapter {
    private int resource;
    private LayoutInflater inflater;
    private List<CountryModel> countryModels;
    private WeakReference<TextView> selectedCountryIdtxt;
    private boolean useFilter;
    private WeakReference<ProgressBar> progressBarWeakReference;

    public int getSelectedCountryId() {
        return selectedCountryId;
    }

    public void setSelectedCountryId(int selectedCountryId) {
        this.selectedCountryId = selectedCountryId;
    }

    private int selectedCountryId;

    public CountriesListAdapter(@NonNull WeakReference<Context> context, int resourceId, WeakReference<TextView> textView, @NonNull List<CountryModel> countryModelList, boolean useFilter, WeakReference<ProgressBar> progressBarWeakReference){
        super(context.get(), resourceId, countryModelList);
        selectedCountryIdtxt = textView;
        resource = resourceId; //the id of the template file
        inflater = LayoutInflater.from(context.get());
        this.countryModels = countryModelList;
        selectedCountryId = -1;
        this.useFilter = useFilter;
        this.progressBarWeakReference = progressBarWeakReference;
    }

    public int getCount() {
        if (countryModels != null)
            return countryModels.size();
        return 0;
    }

    public CountryModel getItem(int position) {
        if (countryModels != null)
            return countryModels.get(position);
        return null;
    }

    public long getItemId(int position) {
        if (countryModels != null)
            return countryModels.get(position).hashCode();
        return 0;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    // this method is automatically called for every object in our list
    //basically it's called for every single row before it is generated
    // this method is called per row
        convertView = (ConstraintLayout) inflater.inflate(resource, null);

    //the variable countryModel is fiiled with current object that is being processed
        final CountryModel countryModel = countryModels.get(position);
        TextView countryName = convertView.findViewById(R.id.countryNamelbl);
        final ImageView countryFlag = convertView.findViewById(R.id.countryFlagimg);
        final ImageView checked = convertView.findViewById(R.id.countryCheckedimg);

    //this is done for every object in the list
        assert countryModel != null;
        countryName.setText(countryModel.getName());
        Picasso.get().load(countryModel.getImage()).fit().into(countryFlag);
        if(!useFilter) {
            if (selectedCountryId == countryModel.getId()) {
                checked.setVisibility(View.VISIBLE);
            } else {
            checked.setVisibility(View.INVISIBLE);
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!useFilter) {
                    if (checked.getVisibility() == View.VISIBLE) {
                        checked.setVisibility(View.INVISIBLE);
                        selectedCountryId = -1;
                            selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
                    } else {
                        if (selectedCountryId == -1) {
                            checked.setVisibility(View.VISIBLE);
                            selectedCountryId = countryModel.getId();
                        } else {
                            selectedCountryId = countryModel.getId();
                            notifyDataSetChanged();
                        }
                    selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
                    }
                } else {
                    Intent i = new Intent(getContext(), PicturesActivity.class);
                    i.putExtra("countryId",countryModel.getId());
                    i.putExtra("countryName", countryModel.getName());
                    getContext().startActivity(i);
               }
           }
       });
       progressBarWeakReference.get().setVisibility(View.INVISIBLE);
       return convertView;
    }

    public List<CountryModel> getCountryModels() {
        return countryModels;
    }

    public void setCountryModels(List<CountryModel> countryModels) {
        this.countryModels = countryModels;
    }
}
公共类CountriesListAdapter扩展了ArrayAdapter{
私有int资源;
私人充气机;
私有列表模型;
私有WeakReference selectedCountryIdtxt;
专用布尔滤波器;
私人WeakReference progressBarWeakReference;
public int getSelectedCountryId(){
返回selectedCountryId;
}
public void setSelectedCountryId(int selectedCountryId){
this.selectedCountryId=selectedCountryId;
}
私有int-selectedCountryId;
public CountriesListAdapter(@NonNull WeakReference上下文、int resourceId、WeakReference文本视图、@NonNull列表countryModelList、布尔useFilter、WeakReference progressBarWeakReference){
super(context.get(),resourceId,countryModelList);
selectedCountryIdtxt=textView;
resource=resourceId;//模板文件的id
inflater=LayoutInflater.from(context.get());
this.countryModels=countryModels列表;
selectedCountryId=-1;
this.useFilter=useFilter;
this.progressBarWeakReference=progressBarWeakReference;
}
public int getCount(){
如果(countryModels!=null)
返回countryModels.size();
返回0;
}
public CountryModel getItem(内部位置){
如果(countryModels!=null)
返回countryModels.get(位置);
返回null;
}
公共长getItemId(int位置){
如果(countryModels!=null)
return countryModels.get(position.hashCode();
返回0;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
//此方法会自动为列表中的每个对象调用
//基本上,在生成每一行之前都会调用它
//此方法按行调用
convertView=(ConstraintLayout)充气器。充气(资源,null);
//变量countryModel与正在处理的当前对象一起归档
最终CountryModel CountryModel=countryModels.get(位置);
TextView countryName=convertView.findViewById(R.id.countryNamelbl);
最终ImageView countryFlag=convertView.findViewById(R.id.countryFlagimg);
选中的最终图像视图=convertView.findViewById(R.id.countryCheckedimg);
//对列表中的每个对象都执行此操作
断言countryModel!=null;
countryName.setText(countryModel.getName());
毕加索.get().load(countryModel.getImage()).fit().into(countryFlag);
如果(!useFilter){
if(selectedCountryId==countryModel.getId()){
选中。设置可见性(视图。可见);
}否则{
选中。设置可见性(视图。不可见);
}
}
convertView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
如果(!useFilter){
if(选中的.getVisibility()==View.VISIBLE){
选中。设置可见性(视图。不可见);
selectedCountryId=-1;
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
}否则{
如果(selectedCountryId==-1){
选中。设置可见性(视图。可见);
selectedCountryId=countryModel.getId();
}否则{
selectedCountryId=countryModel.getId();
notifyDataSetChanged();
}
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
}
}否则{
Intent i=新的Intent(getContext(),PicturesActivity.class);
i、 putExtra(“countryId”,countryModel.getId());
i、 putExtra(“countryName”,countryModel.getName());
getContext().startActivity(i);
}
}
});
progressBarWeakReference.get().setVisibility(View.INVISIBLE);
返回视图;
}
公共列表getCountryModels(){
回归模型;
}
public void setCountryModels(列出countryModels){
this.countryModels=countryModels;
}
}

问题实际上在另一个类中,我为每个列表项调用适配器,而不是只调用一次。。。哎呀。
谢谢你的回复

如果您有大量数据和图像,您应该使用
RecyclerView
而不是
ListView
。您没有向我们展示如何填充数据或在何处设置适配器。您的问题可能是由该代码引起的