在动态分配节值时,HeaderListView给出算术异常

时间:2013-11-06 10:17:18

标签: android android-listview

我是Android的新手,也是HeaderListview的新手。我正在使用HeaderListView库。我面临的问题是,当我在运行时动态添加该部分的值时,它会给我以下错误

11-06 15:28:34.674:E / AndroidRuntime(957):致命异常:主要 11-06 15:28:34.674:E / AndroidRuntime(957):java.lang.ArithmeticException:除以零 11-06 15:28:34.674:E / AndroidRuntime(957):at com.applidium.headerlistview.HeaderListView $ HeaderListViewOnScrollListener.updateScrollBar(HeaderListView.java:215) 11-06 15:28:34.674:E / AndroidRuntime(957):at com.applidium.headerlistview.HeaderListView $ HeaderListViewOnScrollListener.onScroll(HeaderListView.java:115) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:772) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.widget.ListView.layoutChildren(ListView.java:1507) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.widget.AbsListView.onLayout(AbsListView.java:1260) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.view.View.layout(View.java:7175) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 11-06 15:28:34.674:E / AndroidRuntime(957):在android.view.View.layout(View.java:7175)

public class DemoJson extends Activity {


 ArrayList<DateBean> sortArray = new ArrayList<DateBean>();
 String eventName;
 JSONArray jsonArray;
 JSONObject eventobject;
 private Context context;
 private ProgressDialog progress;
 HeaderListView list;




@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

     list = new HeaderListView(this);

     progress =new ProgressDialog(this);
     progress.setTitle("Loading data");
     progress.setMessage("Please Wait...");
     progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

     context=this;
     new FetchData().execute();
       progress.show();     
}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    list.setAdapter( new SectionAdapter() {

        @Override
        public int numberOfSections() {
            // TODO Auto-generated method stub
            return sortArray.size();
        }

        @Override
        public int numberOfRows(int section) {
            // TODO Auto-generated method stub
            return 10;
        }

        @Override
        public View getRowView(int section, int row, View convertView,ViewGroup parent) {
            // TODO Auto-generated method stub
              if (convertView == null) {
                    convertView = (TextView) getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
                }
                ((TextView) convertView).setText("Section " + section + " Row " + row);
                return convertView;
        }

        @Override
        public Object getRowItem(int section, int row) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean hasSectionHeaderView(int section) {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public int getSectionHeaderViewTypeCount() {
            return 2;
        }

        @Override
        public int getSectionHeaderItemViewType(int section) {
            return section % 2;
        }

        @Override
        public View getSectionHeaderView(int section, View convertView,ViewGroup parent) {
            // TODO Auto-generated method stub
             if (convertView == null) {
                    if (getSectionHeaderItemViewType(section) == 0) {
                        convertView = (TextView) getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
                    } else {
                        convertView = getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_2), null);
                    }
                }

                if (getSectionHeaderItemViewType(section) == 0) {
                    ((TextView) convertView).setText("Header for section " + section);
                } else {
                    ((TextView) convertView.findViewById(android.R.id.text1)).setText("Header for section " + section);
                    ((TextView) convertView.findViewById(android.R.id.text2)).setText("Has a detail text field");
                }

                switch (section) {
                case 0:
                    convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
                    break;
                case 1:
                    convertView.setBackgroundColor(getResources().getColor(R.color.holo_orange_light));
                    break;
                case 2:
                    convertView.setBackgroundColor(getResources().getColor(R.color.holo_green_light));
                    break;
                case 3:
                    convertView.setBackgroundColor(getResources().getColor(R.color.holo_blue_light));
                    break;
                case 4: 
                    convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
                }

                return convertView;
        }

         @Override
            public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
                super.onRowItemClick(parent, view, section, row, id);
                Toast.makeText(DemoJson.this, "Section " + section + " Row " + row + " ID " + id, Toast.LENGTH_SHORT).show();
            }

    });


     setContentView(list);
}



     class FetchData extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL

            JSONObject json = jParser.getJSONFromUrl("http:// Url Value");
            try {
                 eventobject = json.getJSONObject("events");

                Iterator<Object> keys = eventobject.keys();

                while (keys.hasNext()) {

                    String datestring = String.valueOf(keys.next());

                    // by below logic you skip missed date.
                    if (datestring.trim().length() > 0) {
                        SimpleDateFormat formatter = new SimpleDateFormat(
                                "yyyy-MM-dd");
                        Date date = formatter.parse(datestring);
                        DateBean dateBean = new DateBean(date);
                        sortArray.add(dateBean);
                    } else {

                    }

                }

                System.out.println("size:" + sortArray.size());

                System.out.println("==========sorting array======");
                Collections.sort(sortArray, new CompareDate());
                // reverse order
                // Collections.reverse(sortArray);

                for (DateBean d : sortArray) {
                    final String dateKey = new SimpleDateFormat("yyyy-MM-dd")
                            .format(d.getDate());
                    System.out.println("here date key:"+dateKey);



                    JSONArray jsonArray = eventobject.getJSONArray(dateKey);
                    System.out.println("DateKey--" + jsonArray);
                    eventName =  new String();
                    for(int i=0 ; i<jsonArray.length() ; i++){

                        JSONObject i_lo= jsonArray.getJSONObject(i);
                        eventName = i_lo.getString("name");
                        System.out.println("name is :" + eventName);
                    }

                }

这里我的sortArray是具有我想要的部分值的arrarylist。

另外我需要在setContentview()中设置一个布局,因为我在listview旁边的布局中还有其他元素。

提前谢谢

1 个答案:

答案 0 :(得分:0)

在设置适配器

之前,可以将标题添加到列表视图中