java - listView 中的 addFooterView 给出 ClassCastException

标签 java android listview android-progressbar

我正在制作一个带有无限 listView 的应用程序,开始时列表只有 10 个元素,但是当用户滚动到末尾时,它会增加 10 个元素,依此类推

我想在最后添加进度条,以便在添加元素之前显示进度条。为此,我像这样使用 addFooterView

 pro = (ProgressBar)rootView.findViewById(R.id.progress);
 ListView lv = ((ListView)rootView.findViewById(R.id.listViewInternship));
   lv.addFooterView(pro);

这个 lv.addFooterView(pro) 给出了以下异常

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

有什么问题??

编辑

我的 xml 文件是

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/progress"
>
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true" /></RelativeLayout>

请大家帮忙

我的完整java代码

public class Internship extends Fragment {

int count = 0;
JSONParser jsonParser = new JSONParser();
public Internship() {
}

//urls of php files
private static final String url_of_showing_all_internships = "some url";

Boolean flag_loading=false;
ArrayList<DataOfInternship> internshipArrayList ;
ArrayAdapter<DataOfInternship> adapter;
ProgressBar pro ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView =inflater.inflate(R.layout.fragment_internship, container, false);

    count = 1;
    pro = (ProgressBar)rootView.findViewById(R.id.progress);
    internshipArrayList =  new ArrayList<>();

    additems();

    adapter = new ArrayAdapter<DataOfInternship>(getActivity() , R.layout.internship_view,R.id.titleOfInternship,internshipArrayList){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);

            ((TextView)row.findViewById(R.id.typeOfInternship)).setText(internshipArrayList.get(position).Type);
            ((TextView)row.findViewById(R.id.titleOfInternship)).setText(internshipArrayList.get(position).Name);
            ((TextView)row.findViewById(R.id.locationOfInternship)).setText(internshipArrayList.get(position).Location);
            ((TextView)row.findViewById(R.id.startDateOfInternship)).setText(internshipArrayList.get(position).StartDate);
            ((TextView)row.findViewById(R.id.stipendOfInternship)).setText(internshipArrayList.get(position).Stipend);
            ((TextView)row.findViewById(R.id.durationOfInternship)).setText(internshipArrayList.get(position).Duration);

            return  row;
        }
    };

    ListView lv = ((ListView)rootView.findViewById(R.id.listViewInternship));
    lv.addFooterView(pro);
    lv.setAdapter(adapter);
    lv.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {


        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {

            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                if(!flag_loading)
                {
                    flag_loading = true;
                    additems();
                    count+=10;
                }
            }
        }
    });

    return rootView;
}JSONArray internships = null;
private void additems() {

    new AsyncTask<Void , Void, Void>(){

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(Void... params) {

            //making parameters for sno
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("sno" , String.valueOf(count)));

            //making json object
            JSONObject json = jsonParser.makeHttpRequest(url_of_showing_all_internships, "POST", parameters);

            Log.e("count= "+count,json.toString());

            try {
                int success = json.getInt("success");
                if (success ==1){
                    internships = json.getJSONArray("internships");

                    for (int i = 0 ; i < internships.length(); i++){
                        JSONObject temp = internships.getJSONObject(i);

                        //making dataobject
                        String title = temp.getString("internship_title")
                                , type = temp.getString("internship_type")
                                ,location = temp.getString("location")
                                ,start_date = temp.getString("start_date")
                                ,duration = temp.getString("duration")
                                ,stipend = temp.getString("stipend")
                                ,description = temp.getString("dis")
                                ;

                        DataOfInternship tempData = new DataOfInternship(title,type,description,"",location,stipend,start_date,duration);
                        internshipArrayList.add(tempData);

                    }
                    flag_loading = false;


                }
                else publishProgress();
            } catch (JSONException e) {
                Log.e("json Exception" , e.toString());
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            Toast.makeText(getActivity(),"Connection not secure",Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            adapter.notifyDataSetChanged();
        }
    }.execute();

}

我知道这很乱,但有助于理解

提前致谢

最佳答案

知道了 谢谢 TinTran 和 Jay Shah 因为你们找到了解决方案

问题出在 lv.addFooterView(专业版)

我应该在其中添加一个 View addFooterView 而不是进度条

所以代码是

View view = inflater.inflate(R.layout.progressbar_view,null,false); lv.addFooterView(view);

代替 lv.addFooterView(pro);

再次感谢大家

关于java - listView 中的 addFooterView 给出 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695983/

相关文章:

Android ListView按下后退按钮后重复数据

java - 如何更改 JTable 中文本段落的颜色?

java - 与java中的.txt文件相比,读取/写入一组到.ser文件

android - Listview 只显示列表中的一行

android - 根据android中按钮点击的持续时间触发不同的方法

java - Android中如何从ListView中获取当前所选项目的背景颜色

java - Spring Boot 中 URL 必须以 'jdbc' 开头

java - 为什么我的方法在 System.in.read() 循环后被调用 3 次

android - View 的 setContentDescription() 的目的是什么?

java - 在微调器上选择项目时更新 ListView