关闭

二级列表

88人阅读 评论(0) 收藏 举报
分类:

package com.bwie.main;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

import com.bwie.dao.Dao;
import com.bwie.jichu.DataJichu;
import com.bwie.jichu.Jichu;
import com.example.android_00_monthexam_gson.R;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

public class Fragment1 extends Fragment {
private List jichu = new ArrayList();
private List groupjichu = new ArrayList();
private List(List(String)) childjichu = new ArrayList();
private ExpandableListView exlv;
private Dao dao;
private boolean add;
private SharedPreferences spfe;

//二级列表 点击一个开一个
private int lastClick = -1;//上一次点击的group的position  

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View f1 = View.inflate(getActivity(), R.layout.f1, null);
    spfe = getActivity().getSharedPreferences("1409A1", 1);
    add = spfe.getBoolean("add", true);
    dao = new Dao(getActivity());
    ShowJichu();
    exlv = (ExpandableListView) f1.findViewById(R.id.exlv1);
    exlv.setAdapter(new Myadapter());
    exlv.setOnGroupClickListener(new OnGroupClickListener() {  
        public boolean onGroupClick(ExpandableListView parent, View v,  
                 int groupPosition, long id) {  

           if(lastClick == -1)  
           {  
               exlv.expandGroup(groupPosition);  
            }  

            if(lastClick != -1 && lastClick != groupPosition)  
            {  
                exlv.collapseGroup(lastClick);  
                exlv.expandGroup(groupPosition);  
             }  
             else if(lastClick == groupPosition)   
             {  
                 if(exlv.isGroupExpanded(groupPosition))  
                     exlv.collapseGroup(groupPosition);  
                 else if(!exlv.isGroupExpanded(groupPosition))  
                     exlv.expandGroup(groupPosition);  
             }  

             lastClick = groupPosition;  
             return true;  
         }  
     });  
    return f1;
}

// 解析Gson
public void ShowJichu() {
    Gson gson = new Gson();
    try {
        DataJichu datajichu = gson.fromJson(new InputStreamReader(
                getActivity().getAssets().open("base.json"), "gbk"),
                DataJichu.class);
        jichu = datajichu.getData();

        for (Jichu ji : jichu) {
            // 第一个集合
            groupjichu.add(ji.getTitle());
            // 第二个集合
            List<String> list = new ArrayList<String>();
            list.add(ji.getReference());
            childjichu.add(list);
            if (add) {
                ContentValues values = new ContentValues();
                values.put("timu", ji.getTitle());
                values.put("daan", ji.getReference());
                values.put("biaoji", "基础");
                dao.insert(values);
                Editor editor = spfe.edit();
                editor.putBoolean("add", false);
                editor.commit();
            }
        }
    } catch (JsonSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonIOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

// 适配器
class Myadapter extends BaseExpandableListAdapter {

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

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childjichu.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        convertView = View
                .inflate(getActivity(), R.layout.exlv_item1, null);
        TextView tv_title = (TextView) convertView
                .findViewById(R.id.tv_titile);
        tv_title.setText(groupjichu.get(groupPosition));
        convertView.setBackgroundResource(R.drawable.listview_bg);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        convertView = View
                .inflate(getActivity(), R.layout.exlv_item2, null);
        TextView tv_reference = (TextView) convertView
                .findViewById(R.id.tv_reference);
        tv_reference.setText(childjichu.get(groupPosition).get(
                childPosition));
        convertView.setBackgroundResource(R.drawable.guide_pop);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11330次
    • 积分:341
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档