• 二级列表展示数据库查询


    package com.bawei.lian2;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    import com.bawei.vo.Good;
    import com.bawei.vo.Info;
    import com.google.gson.Gson;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    
    public class MainActivity extends Activity {
        public static final String URL="http://www.hengboit.com/json/json_search.php?wd=";
        private EditText editText;
        private Button button;
        private ExpandableListView elv;
        private SQLiteDatabase db;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            editText = (EditText) findViewById(R.id.edittext);
            button = (Button) findViewById(R.id.button);
            elv = (ExpandableListView) findViewById(R.id.elv);
            MySqliteHelper helper=new MySqliteHelper(MainActivity.this,"ss",null,1);
             db = helper.getWritableDatabase();
             button.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String name = editText.getText().toString();
                    init(name);
                }
            });
           
        }
        private void init(final String name) {
            // TODO Auto-generated method stub
            new Thread(){
                private Set<String> set;
    
                public void run() {
                    String st=NetWork.geturl("http://www.hengboit.com/json/json_search.php?wd="+name);
                   Gson gson=new Gson();
                   Good good = gson.fromJson(st, Good.class);
                   List<Info> info = good.getInfo();
                   set = new HashSet<String>();
                   for(int i=0;i<info.size();i++){
                       set.add(info.get(i).getCtitle());
                   }
                   
                  final List<String> list=new ArrayList<String>();
                   list.addAll(set);
                   for(int i=0;i<list.size();i++){
                    String s = list.get(i);
                    db.execSQL("insert into user values(null,?)",new Object[]{s});
                   } 
                   for(int i=0;i<info.size();i++){
                        String cid = info.get(i).getCid();
                        String ctime = info.get(i).getCtime();
                        String ctitle = info.get(i).getCtitle();
                        String id = info.get(i).getId();
                        String descript = info.get(i).getDescript();
                        String picurl = info.get(i).getPicurl();
                        String title = info.get(i).getTitle();
                        String type = info.get(i).getType();
                        db.execSQL("insert into good values(?,?,?,?,?,?,?,?)",new Object[]{id,cid,ctime,ctitle, descript,picurl,title,type});
                     
                       }
                   runOnUiThread( new Runnable() {
                    public void run() {
                        Shipei adapter=new Shipei(MainActivity.this,list);
                        elv.setAdapter(adapter);
                        elv.setOnChildClickListener(new OnChildClickListener() {
                            
                            @Override
                            public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
                                // TODO Auto-generated method stub
                                
                                return false;
                            }
                        });
                    }
                });
                };
            }.start();
        }
         
        
        
    }
    package com.bawei.lian2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.bawei.vo.Info;
    import com.lidroid.xutils.BitmapUtils;
    
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class Shipei extends BaseExpandableListAdapter{
        Context context;
        List<String> list;
        private SQLiteDatabase db;
        
        public Shipei(Context context, List<String> list) {
            // TODO Auto-generated constructor stub
         this.context=context;
         this.list=list;
         MySqliteHelper helper=new MySqliteHelper(context,"ss",null,1);
         db = helper.getWritableDatabase();
       }
    
        
    
    
    
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            String name = list.get(groupPosition);
            Cursor c = db.rawQuery("select * from good where ctitle=?", new String[]{name});
            List<Info> li=new ArrayList<Info>();
            while(c.moveToNext()){
              String cid=c.getString(c.getColumnIndex("cid"));
              String ctime=c.getString(c.getColumnIndex("ctime"));
              String ctitle=c.getString(c.getColumnIndex("ctitle"));
              String descript=c.getString(c.getColumnIndex("descript"));
              String id=c.getString(c.getColumnIndex("id"));
              String picurl=c.getString(c.getColumnIndex("picurl"));
              String title=c.getString(c.getColumnIndex("title"));
              String type=c.getString(c.getColumnIndex("type"));
              li.add(new Info(cid, ctime, ctitle, descript, id, picurl, title, type));
            }
            View view=View.inflate(context, R.layout.bu, null);
            ImageView img=(ImageView) view.findViewById(R.id.img);
            TextView tv_title=(TextView) view.findViewById(R.id.tv_title);
            TextView tv_desc=(TextView) view.findViewById(R.id.tv_desc);
            TextView tv_time=(TextView) view.findViewById(R.id.tv_time);
             Info in = li.get(childPosition);
             tv_title.setText(in.getTitle());
                tv_desc.setText(in.getDescript());    
                tv_time.setText(in.getCtitle());
                
                BitmapUtils bit=new BitmapUtils(context);
                bit.display(img,in.getPicurl());
    
            return view;
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            String name = list.get(groupPosition);
            Cursor c = db.rawQuery("select * from good where ctitle=?", new String[]{name});
            return c.getCount();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            Cursor c = db.rawQuery("select * from user ", new String[]{});
            return c.getCount();
            
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Cursor c = db.rawQuery("select * from user ", new String[]{});
            List<String> l=new ArrayList<String>();
            while(c.moveToNext()){
                String st = c.getString(c.getColumnIndex("ctitle"));
                l.add(st);
            }
            View view=View.inflate(context, android.R.layout.simple_expandable_list_item_1, null);
            TextView textView=(TextView) view.findViewById(android.R.id.text1);
            textView.setText(l.get(groupPosition));
            return view;
        }
    
        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }
    
    }
    package com.bawei.lian2;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MySqliteHelper extends SQLiteOpenHelper{
    
        public MySqliteHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
        db.execSQL("create table user(id integer primary key autoincrement, ctitle varchar(50))");    
        db.execSQL("create  table good(id varchar(50) , cid varchar(50),ctime varchar(50),ctitle varchar(50),descript varchar(100),picurl varchar(100), title varchar(100),type varchar(50))");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            
        }
    
    }
  • 相关阅读:
    NIO(二)
    前端面试题整理及答案
    win10休眠后屏幕唤不醒
    Python 将多个列表相同索引的元素进行拼接并输出
    Airtest 常用方法
    windows 各种巨强工具包
    mysql 主从数据同步配置
    windows 10 21H1 顶部任务栏点击音量或其他图标不出弹框
    html5 tts(文字朗读)
    为 map 中不存在的 key 提供缺省值
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5158717.html
Copyright © 2020-2023  润新知