package com.example.zuoyeone.MySqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySqlHelp extends SQLiteOpenHelper {
public MySqlHelp(@Nullable Context context) {
super(context, "school.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table person(Picld integer,PicUrl varchar(30))");
sqLiteDatabase.beginTransaction();
sqLiteDatabase.setTransactionSuccessful();
sqLiteDatabase.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
package com.example.zuoyeone;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListView;
import com.example.zuoyeone.MySqlite.MySqlHelp;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
private ListView listView;
private List<JavaBean.DataBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private MyAsTask myAsTask;
private Uri uri ;
private SQLiteDatabase db;
private MyProvider myProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.lv);
uri = Uri.parse("content://com.w.uri");
myProvider = new MyProvider();
MySqlHelp mySqlHelp = new MySqlHelp(this);
db = mySqlHelp.getReadableDatabase();
new MyAsTask(list,db,myProvider,uri).execute(path);
for (int i = 0; i < list.size(); i++) {
ContentValues contentValues = new ContentValues();
contentValues.put("Picld",list.get(i).getId());
contentValues.put("PicUrl",list.get(i).getPic());
myProvider.insert(uri,contentValues);
}
}
}
package com.example.zuoyeone;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<JavaBean.DataBean> list;
private Context context;
public MyAdapter(List<JavaBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null){
view = LayoutInflater.from(context).inflate(R.layout.item,null);
holder = new ViewHolder();
holder.foodIv = (ImageView) view.findViewById(R.id.food_iv);
holder.foodName = (TextView) view.findViewById(R.id.foodName_tv);
holder.foodS = (TextView) view.findViewById(R.id.foodStr_tv);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
JavaBean.DataBean bean = list.get(i);
holder.foodName.setText(bean.getTitle());
holder.foodS.setText(bean.getPic());
Glide.with(context).load(bean.getPic()).into(holder.foodIv);
return view;
}
static class ViewHolder{
ImageView foodIv;
TextView foodName;
TextView foodS;
}
}
package com.example.zuoyeone;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.util.List;
public class MyAsTask extends AsyncTask<String,Void,String> {
private List<JavaBean.DataBean> list;
private SQLiteDatabase db;
private MyProvider myProvider;
private Uri uri;
public MyAsTask(List<JavaBean.DataBean> list, SQLiteDatabase db, MyProvider myProvider, Uri uri) {
this.list = list;
this.db = db;
this.myProvider = myProvider;
this.uri = uri;
}
@Override
protected String doInBackground(String... strings) {
return Utils.getJson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (!s.isEmpty()){
Gson gson = new Gson();
JavaBean javaBean = gson.fromJson(s, JavaBean.class);
List<JavaBean.DataBean> data = javaBean.getData();
list.addAll(data);
for (int i = 0; i < javaBean.getData().size(); i++) {
String id = javaBean.getData().get(i).getId();
String pic = javaBean.getData().get(i).getPic();
String sql = "insert into person values (?,?)";
db.execSQL(sql,new Object[]{id,pic});
}
}
}
}
package com.example.zuoyeone;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.example.zuoyeone.MySqlite.MySqlHelp;
public class MyProvider extends ContentProvider {
private SQLiteDatabase db;
public MyProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return db.delete("person",selection,selectionArgs);
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db.insert("person",null,values);
return uri;
}
@Override
public boolean onCreate() {
MySqlHelp mySqlHelp = new MySqlHelp(getContext());
db = mySqlHelp.getReadableDatabase();
if (db == null){
return false;
}
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return db.query("person",projection,selection,selectionArgs,null,null,sortOrder);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return db.update("person",values,selection,selectionArgs);
}
}
package com.example.zuoyeone;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Utils {
public static String getJson(String path){
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode()==200){
InputStream is = connection.getInputStream();
byte[] b = new byte[1024];
int len = 0;
StringBuffer sb = new StringBuffer();
while((len = is.read(b))!=-1){
sb.append(new String(b,0,len));
}
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ContentResolver contentResolver;
private Uri uri;
private ListView listView;
private List<JavaBean> list = new ArrayList<>();
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.lv);
uri = Uri.parse("content://com.w.uri");
contentResolver = getContentResolver();
Cursor query = contentResolver.query(uri, null, null, null, null);
if (query != null) {
while (query.moveToNext()) {
String picId = query.getString(query.getColumnIndex("Picld"));
String picUrl = query.getString(query.getColumnIndex("PicUrl"));
JavaBean javaBean = new JavaBean(picId, picUrl);
Log.i(TAG, "onCreate: " + picId + "---" + picUrl);
list.add(javaBean);
}
}
listView.setAdapter(new BaseAdapter() {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = layoutInflater.inflate(R.layout.item, null);
viewHolder.iv = view.findViewById(R.id.iv);
viewHolder.tv = view.findViewById(R.id.tv);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
// viewHolder.iv.setImageResource(R.mipmap.ic_launcher);
Glide.with(MainActivity.this).load(list.get(i).getPic()).into(viewHolder.iv);
viewHolder.tv.setText(list.get(i).getId());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.remove(i);
notifyDataSetChanged();
String id = list.get(i).getId();
Cursor cursor = contentResolver.query(uri, new String[]{id}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
contentResolver.delete(uri, " Picld = ?", new String[]{id});
}
}
}
});
return view;
}
class ViewHolder {
private ImageView iv;
private TextView tv;
}
});
}
}