main
package com.example.zhoukao_01_mn;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.zhoukao_01_mn.adapter.MyAdapter;
import com.example.zhoukao_01_mn.bean.MyData;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivityextends AppCompatActivity {
private ListViewlv;
private StringmUrl="https://suggest.taobao.com/sug?code=utf-8&q=%E6%89%8B%E6%9C%BA";
private MyAdaptermyAdapter;
private ContentResolverresolver;
private Uriuri = Uri.parse("content://com.example.zhoukao_01_mn.provider.provider.MyContentprovider/user");
private List>mList =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
resolver = getContentResolver();
myAdapter =new MyAdapter(MainActivity.this,mList);
lv.setAdapter(myAdapter);
if(isNetworkConnected(MainActivity.this)){
Cursor cursor =resolver.query(uri,null,null,null,null);
if(cursor.getCount()==0){
new MyToack().execute(mUrl);
}else{
Toast.makeText(MainActivity.this,"有网,已经请求过数据并且保存到了数据库",Toast.LENGTH_SHORT).show();
Cursor query =resolver.query(uri,null,null,null,null);
while (query.moveToNext()){
String string = query.getString(query.getColumnIndex("string"));
mList.add(Collections.singletonList(string));
}
myAdapter.setmList(mList);
}
}else{
Toast.makeText(MainActivity.this,"没有联网,从数据库请求数据",Toast.LENGTH_SHORT).show();
Cursor cursorw =resolver.query(uri,null,null,null,null);
if(cursorw.getCount()==0){
Toast.makeText(MainActivity.this,"数据库没有数据",Toast.LENGTH_SHORT).show();
}else {
while (cursorw.moveToNext()) {
String string = cursorw.getString(cursorw.getColumnIndex("string"));
mList.add(Collections.singletonList(string));
}
// Log.d("zzz",mList.toString());
myAdapter.setmList(mList);
}
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,final int position,long id) {
AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
builder.setTitle("确认删除吗?");
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
}
});
builder.setPositiveButton("确认",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// Toast.makeText(MainActivity.this,mList.get(position).toString()+"",Toast.LENGTH_SHORT).show();
resolver.delete(uri,"string=?",new String[]{mList.get(position).toString()});
Toast.makeText(MainActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();
mList.remove(position);
myAdapter.notifyDataSetChanged();
}
});
builder.show();
}
});
}
public boolean isNetworkConnected(Context context) {
if (context !=null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo !=null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
}
class MyToackextends AsyncTask>>{
@Override
protected List> doInBackground(String... strings) {
try {
URL url =new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection.getResponseCode()==200){
InputStream stream = connection.getInputStream();
Gson gson =new Gson();
MyData myData = gson.fromJson(new BufferedReader(new InputStreamReader(stream)), MyData.class);
mList=myData.getResult();
for(int i=0;i
ContentValues values =new ContentValues();
Log.d("zzz",mList.get(i).toString());
values.put("string",mList.get(i).toString());
resolver.insert(uri,values);
}
return mList;
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List> lists) {
super.onPostExecute(lists);
myAdapter.setmList(lists);
}
}
}
内容提供者
package com.example.zhoukao_01_mn.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.example.zhoukao_01_mn.Helper.MyHelper;
import com.example.zhoukao_01_mn.MainActivity;
public class MyContentproviderextends ContentProvider {
private SQLiteDatabasedb;
private static UriMatcherum =new UriMatcher(UriMatcher.NO_MATCH);
static {
um.addURI(".provider.provider.MyContentprovider","user",100);
}
@Override
public boolean onCreate() {
MyHelper helper =new MyHelper(getContext());
db = helper.getWritableDatabase();
return false;
}
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor =db.query("user",null,null,null,null,null,null);
return cursor;
}
@Override
public String getType( Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db.insert("user",null,values);
return null;
}
@Override
public int delete( Uri uri, String selection, String[] selectionArgs) {
int i =db.delete("user", selection, selectionArgs);
return i;
}
@Override
public int update( Uri uri, ContentValues values, String selection,String[] selectionArgs) {
return 0;
}
}
helper
package com.example.zhoukao_01_mn.Helper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelperextends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context,"show",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql ="create table if not exists user(string text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
}
}
adapter
package com.example.zhoukao_01_mn.adapter;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.zhoukao_01_mn.R;
import java.util.List;
public class MyAdapterextends BaseAdapter {
private Contextcontext;
private List>mList;
public MyAdapter(Context context, List> mList) {
this.context = context;
this.mList = mList;
}
public void setmList(List> mList){
this.mList=mList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView ==null){
convertView=View.inflate(context, R.layout.list_item,null);
holder =new ViewHolder();
holder.tv=convertView.findViewById(R.id.tv);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(mList.get(position).toString());
return convertView;
}
class ViewHolder{
TextViewtv;
}
}