sql语句的常规使用

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private TextView tv;
private DBHepler dbHepler;
private List<Book> bookList;
private ListView listView;
private MyAdapter myAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findViewById(R.id.tv);
    listView = findViewById(R.id.lv);
    dbHepler = DBHepler.getInstance(this);
    bookList = new ArrayList<>();
    requesData();
    myAdapter = new MyAdapter(this, bookList);
    listView.setAdapter(myAdapter);


}

String data111 = "{\n" +
        "\t\"employees\": [{\n" +
        "\t\t\t\"name\": \"John\",\n" +
        "\t\t\t\"price\": \"Doe\",\n" +
        "\t\t\t\"address\": \"http://img.zcool.cn/community/0195be57cd70d90000012e7eb0dae8.jpg@1280w_1l_2o_100sh.jpg\"\n" +
        "\t\t},\n" +
        "\t\t{\n" +
        "\t\t\t\"name\": \"John222\",\n" +
        "\t\t\t\"price\": \"Doe333\",\n" +
        "\t\t\t\"address\": \"http://img.zcool.cn/community/0195be57cd70d90000012e7eb0dae8.jpg@1280w_1l_2o_100sh.jpg\"\n" +
        "\t\t},\n" +
        "\t\t{\n" +
        "\t\t\t\"name\": \"John1111\",\n" +
        "\t\t\t\"price\": \"Doe1111\",\n" +
        "\t\t\t\"address\": \"http://img.zcool.cn/community/0195be57cd70d90000012e7eb0dae8.jpg@1280w_1l_2o_100sh.jpg\"\n" +
        "\t\t}\n" +
        "\t]\n" +
        "}";
private void requesData() {
    try {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://www.baidu.com")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i(TAG, "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) {
                try {
                    String data = response.body().string();
                    JSONObject jsonObject = new JSONObject(data111);
                    JSONArray jsonArray = jsonObject.getJSONArray("employees");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject json = (JSONObject) jsonArray.get(i);
                        String name = json.getString("name");
                        String price = json.getString("price");
                        String address = json.getString("address");
                        save(name, price, address);
                    }
                    getData();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void save(String name, String price, String address) {
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("price", price);
    contentValues.put("address", address);
    dbHepler.getWritableDatabase().insert("Book", null, contentValues);
}

public void getData() {
    Cursor cursor = dbHepler.getWritableDatabase().query("Book", null, null, null, null, null, null);
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String price = cursor.getString(cursor.getColumnIndex("price"));
        String address = cursor.getString(cursor.getColumnIndex("address"));
        bookList.add(new Book(address, price, name));
    }
    myAdapter.notifyDataSetChanged();
}

class MyAdapter extends BaseAdapter {
    Context context;
    List<Book> list;

    public MyAdapter(Context context, List<Book> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Book getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LinearLayout.inflate(context, R.layout.item, null);
            viewHolder.tv1 = convertView.findViewById(R.id.tv1);
            viewHolder.tv2 = convertView.findViewById(R.id.tv2);
            viewHolder.iv = convertView.findViewById(R.id.iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Book book = list.get(position);
        viewHolder.tv1.setText(book.getName());
        viewHolder.tv2.setText(book.getPrice());
        Glide.with(context).load(book.getAddress()).into(viewHolder.iv);
        return convertView;
    }

    class ViewHolder {
        TextView tv1, tv2;
        ImageView iv;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值