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;
}
}