Android HeaderListView在动态分配节值时提供算术异常
我不熟悉android,也不熟悉HeaderListview。我正在使用HeaderListView库。我所面临的问题是,当我在运行时动态地为该节添加值时,会出现以下错误 11-06 15:28:34.674:E/AndroidRuntime(957):致命异常:main 11-06 15:28:34.674:E/AndroidRuntime(957):java.lang.arithmetricException:被零除 11-06 15:28:34.674:E/AndroidRuntime(957):在com.applidium.headerlistview.headerlistview$HeaderListViewOnScrollListener.updateScrollBar(headerlistview.java:215) 11-06 15:28:34.674:E/AndroidRuntime(957):在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)Android HeaderListView在动态分配节值时提供算术异常,android,android-listview,Android,Android Listview,我不熟悉android,也不熟悉HeaderListview。我正在使用HeaderListView库。我所面临的问题是,当我在运行时动态地为该节添加值时,会出现以下错误 11-06 15:28:34.674:E/AndroidRuntime(957):致命异常:main 11-06 15:28:34.674:E/AndroidRuntime(957):java.lang.arithmetricException:被零除 11-06 15:28:34.674:E/AndroidRuntime(9
public类DemoJson扩展活动{
ArrayList sortArray=新的ArrayList();
字符串eventName;
JSONArray JSONArray;
JSONObject事件对象;
私人语境;
私人进展对话进展;
标题列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
列表=新的HeaderListView(此);
进度=新建进度对话框(此对话框);
进度。设置标题(“加载数据”);
progress.setMessage(“请稍候…”);
progress.setProgressStyle(ProgressDialog.STYLE\u微调器);
上下文=这个;
新建FetchData().execute();
progress.show();
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
super.onResume();
list.setAdapter(新的SectionAdapter(){
@凌驾
公共int numberOfSections(){
//TODO自动生成的方法存根
返回sortArray.size();
}
@凌驾
公共整数行数(整数部分){
//TODO自动生成的方法存根
返回10;
}
@凌驾
公共视图getRowView(int段、int行、视图转换视图、视图组父级){
//TODO自动生成的方法存根
if(convertView==null){
convertView=(TextView)GetLayoutFlater().inflate(getResources().getLayout(android.R.layout.simple\u list\u item\u 1),null);
}
(TextView)convertView.setText(“节”+节+“行”+行);
返回视图;
}
@凌驾
公共对象getRowItem(int部分,int行){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共布尔值hasSectionHeaderView(内部部分){
//TODO自动生成的方法存根
返回true;
}
@凌驾
public int getSectionHeaderServiceTypeCount(){
返回2;
}
@凌驾
公共int getSectionHeaderItemViewType(int节){
返回节%2;
}
@凌驾
公共视图getSectionHeaderView(int节、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
if(convertView==null){
如果(getSectionHeaderItemViewType(section)==0){
convertView=(TextView)GetLayoutFlater().inflate(getResources().getLayout(android.R.layout.simple\u list\u item\u 1),null);
}否则{
convertView=GetLayoutFlater().inflate(getResources().getLayout(android.R.layout.simple\u list\u item\u 2),null);
}
}
如果(getSectionHeaderItemViewType(section)==0){
(TextView)convertView.setText(“节头”+节头);
}否则{
((TextView)convertView.findviewbyd(android.R.id.text1)).setText(“节头”+节头);
((TextView)convertView.findviewbyd(android.R.id.text2)).setText(“具有详细文本字段”);
}
道岔(区段){
案例0:
setBackgroundColor(getResources().getColor(R.color.holo_red_light));
打破
案例1:
setBackgroundColor(getResources().getColor(R.color.holo_orange_light));
打破
案例2:
setBackgroundColor(getResources().getColor(R.color.holo_green_light));
打破
案例3:
setBackgroundColor(getResources().getColor(R.color.holo_blue_light));
打破
案例4:
setBackgroundColor(getResources().getColor(R.color.holo_red_light));
}
返回视图;
}
@凌驾
public void onRowItemClick(AdapterView父项、视图、整型节、整型行、长id){
super.onRowItemClick(父项、视图、节、行、id);
Toast.makeText(DemoJson.this,“Section”+Section+“Row”+Row+“ID”+ID,Toast.LENGTH_SHORT).show();
}
});
setContentView(列表);
}
类FetchData扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
保护空隙d
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);
}
}