listView中的addFooterView提供了ClassCastException

时间:2016-06-08 07:30:53

标签: 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();

}

}

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

提前致谢

1 个答案:

答案 0 :(得分:1)

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

问题在于

@Entity
@Table(name = "RULE_TEST")
public class RuleTestEntity implements Serializable {
private static final long serialVersionUID = -4208222848601642508L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "RULE_ID", nullable = false)
@XmlElement(name = Identifiable.ID_FIELD_NAME)
private long id;
@ManyToOne
@JoinColumn(name = "ACTIVITY_ID", nullable = true, updatable = false)
@XmlTransient
private ActivityTestEntity activity;
public ActivityTestEntity getActivity() {
    return activity;
}
public void setActivity(ActivityTestEntity activity) {
    this.activity = activity;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    RuleTestEntity that = (RuleTestEntity) o;
    if (id != that.id) {
        return false;
    }
    return activity != null ? activity.equals(that.activity) : that.activity == null;
}
@Override
public int hashCode() {
    int result = (int) (id ^ (id >>> 32));
    result = 31 * result + (activity != null ? activity.hashCode() : 0);
    return result;
}

}

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

所以代码是

@Entity @Table(name = "RULE_TEST") public class RuleTestEntity implements Serializable { private static final long serialVersionUID = -4208222848601642508L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "RULE_ID", nullable = false) @XmlElement(name = Identifiable.ID_FIELD_NAME) private long id; @ManyToOne @JoinColumn(name = "ACTIVITY_ID", nullable = true, updatable = false) @XmlTransient private ActivityTestEntity activity; public ActivityTestEntity getActivity() { return activity; } public void setActivity(ActivityTestEntity activity) { this.activity = activity; } public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RuleTestEntity that = (RuleTestEntity) o; if (id != that.id) { return false; } return activity != null ? activity.equals(that.activity) : that.activity == null; } @Override public int hashCode() { int result = (int) (id ^ (id >>> 32)); result = 31 * result + (activity != null ? activity.hashCode() : 0); return result; }

而不是

再一次感谢大家