@[toc] 10.15作业
APP1
activity
package com.example.day1015_homework;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.day1015_homework.javaBean.Food;
import com.example.day1015_homework.javaBean.MyAdapter;
import com.example.day1015_homework.javaBean.MyAsyncTask;
import java.util.ArrayList;
import java.util.List;
import static com.example.day1015_homework.provider.MyProvider.database;
public class MainActivity extends AppCompatActivity {
private List<Food.DataBean> lists = new ArrayList<>();
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadNetWork();
}
private void loadNetWork() {
new MyAsyncTask(lists).execute(path);
}
}
自定义帮助类
package com.example.day1015_homework.Sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class MySqlHelper extends SQLiteOpenHelper {
public MySqlHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table pics (id integer,url varchar(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
内容提供者
package com.example.day1015_homework.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.example.day1015_homework.Sql.MySqlHelper;
public class MyProvider extends ContentProvider {
private static MySqlHelper helper;
public static SQLiteDatabase database;
public MyProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return database.delete("pics",selection,selectionArgs);
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
database.insert("pics",null,values);
return uri;
}
@Override
public boolean onCreate() {
helper = new MySqlHelper(getContext(), "Pics.db", null, 1);
database = helper.getReadableDatabase();
if(database == null){
return false;
}
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return database.query("pics", projection, selection, selectionArgs, sortOrder, null, null);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return update(uri,values,selection,selectionArgs);
}
}
网络请求
package com.example.day1015_homework.javaBean;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Http_Utils {
public static String getJsonFromUrl(String path){
try {
//创建连接
URL url = new URL(path);
//获取连接
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//设置连接方式
connection.setRequestMethod("GET");
//设置连接和读取超时时间
connection.setReadTimeout(3*1000);
connection.setConnectTimeout(3*1000);
//判断连接状态
if(connection.getResponseCode() == 200){
// 创建流
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//创建一个int数据和一个字节数组
int len ;
byte[] bytes = new byte[1024];
//循环读取
while((len = is.read(bytes)) != -1){
//读取数据
baos.write(bytes,0,len);
//刷新
baos.flush();
}
//类型转换 转成字符串
String s = baos.toString();
//关流
is.close();
baos.close();
//返回值
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
异步任务
package com.example.day1015_homework.javaBean;
import android.content.ContentValues;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
import java.util.List;
import static com.example.day1015_homework.provider.MyProvider.database;
public class MyAsyncTask extends AsyncTask<String,Void, List<Food.DataBean>> {
private static final String TAG = "MyAsyncTask";
private List<Food.DataBean> lists ;
public MyAsyncTask(List<Food.DataBean> lists) {
this.lists = lists;
}
@Override
protected List<Food.DataBean> doInBackground(String... strings) {
//调用网络操作
String json = Http_Utils.getJsonFromUrl(strings[0]);
//Gson 解析
List<Food.DataBean> data = new Gson().fromJson(json, Food.class).getData();
for (int i = 0; i < data.size(); i++) {
//获取两个值
int id = Integer.parseInt(data.get(i).getId());
String pic = data.get(i).getPic();
//创建value对象
ContentValues values = new ContentValues();
values.put("id", id);
values.put("url", pic);
//添加进数据库表格
database.insert("pics", null, values);
}
//判断是否为空
if(data == null){
Log.d(TAG, "doInBackground: 数据获取失败!!!!");
}
//返回数据
return data;
}
// @Override
// protected void onPostExecute(List<Food.DataBean> dataBeans) {
// super.onPostExecute(dataBeans);
//
// //添加进集合
// lists.addAll(dataBeans);
// //通知适配器刷新
// adapter.notifyDataSetChanged();
//
// }
}
app2 解析者
package com.example.app2;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<MyUrl> lists = new ArrayList<>();
private ListView listView;
private MyAdapter adapter;
private Uri uri;
private ContentResolver resolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
resolver = getContentResolver();
uri = Uri.parse("content://com.ming.hw");
Cursor cursor = resolver.query(uri, null, null, null, null);
if(cursor != null){
while (cursor.moveToNext()){
String url = cursor.getString(cursor.getColumnIndex("url"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
MyUrl myUrl = new MyUrl();
myUrl.setId(id);
myUrl.setUrl(url);
lists.add(myUrl);
}
}
cursor.close();
initView();
}
private void initView() {
adapter = new MyAdapter(lists, this);
listView.setAdapter(adapter);
listView.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.setMessage("您确定要删除吗?");
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String id1 = String.valueOf(lists.get(position).getId());
resolver.delete(uri, "id = ?", new String[]{id1});
lists.remove(position);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
适配器
package com.example.app2;
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<MyUrl> lists;
private Context context;
public MyAdapter(List<MyUrl> lists, Context context) {
this.lists = lists;
this.context = context;
}
//重写BaseAdapter的四个方法
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//封装对象
ViewHolder holder = null;
//判断convertView是否为空
if (convertView == null) {
//创建对象
holder = new ViewHolder();
//添加视图
convertView = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
//获取控件
holder.imageView = convertView.findViewById(R.id.im_item);
holder.textView = convertView.findViewById(R.id.tv_item);
//添加标记
convertView.setTag(holder);
} else {
//获取标记
holder = (ViewHolder) convertView.getTag();
}
String id = String.valueOf(lists.get(position).getId());
//为控件赋值
holder.textView.setText(id);
Glide.with(context).load(lists.get(position).getUrl()).into(holder.imageView);
return convertView;
}
//封装对象
private class ViewHolder {
private TextView textView;
private ImageView imageView;
}
}
自定义类对象
package com.example.app2;
public class MyUrl {
private int id;
private String url;
public MyUrl(int id, String url) {
this.id = id;
this.url = url;
}
public MyUrl() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "MyUrl{" +
"id=" + id +
", url='" + url + '\'' +
'}';
}
}
app3 电话
contact 对象
package com.example.app3;
class Contact {
private String name;
private String number;
public Contact(String name, String number) {
this.name = name;
this.number = number;
}
public Contact() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Contact{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
'}';
}
}
activity
package com.example.app3;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ContentResolver resolver;
private List<Contact> lists = new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, 100);
} else {
}
getContacts();
adapter = new MyAdapter(lists,this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+lists.get(position).getNumber()));
startActivity(intent);
}
});
}
private void getContacts() {
resolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = resolver.query(uri, null, null, null, null);
if(cursor != null){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact contact = new Contact();
contact.setName(name);
contact.setNumber(number);
lists.add(contact);
}
}
}
}
适配器
package com.example.app3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
class MyAdapter extends BaseAdapter {
private List<Contact> lists ;
private Context context;
public MyAdapter(List<Contact> lists, Context context) {
this.lists = lists;
this.context = context;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.layout_item,null);
holder.textView1 = convertView.findViewById(R.id.text_item);
holder.textView2 = convertView.findViewById(R.id.text_item1);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(lists.get(position).getName());
holder.textView2.setText(lists.get(position).getNumber());
return convertView;
}
private class ViewHolder {
private TextView textView1,textView2;
}
}
内容提供者
package com.example.app3;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyProvider extends ContentProvider {
private SQLiteDatabase database;
public MyProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return database.delete("contacts", selection, selectionArgs);
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
database.insert("contacts", null, values);
return uri;
}
@Override
public boolean onCreate() {
MySqlHelper helper = new MySqlHelper(getContext(), "Contacts.db", null, 1);
database = helper.getReadableDatabase();
if (database != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return database.query("contacts", projection, selection, selectionArgs, null, null, sortOrder);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return database.update("contacts", values, selection, selectionArgs);
}
}
自定义帮助类
package com.example.app3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class MySqlHelper extends SQLiteOpenHelper {
public MySqlHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table contacts (name varchar(20),number varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
图片浏览器
package com.example.app3;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button button1;
private GridView gvId;
private List<String> names = new ArrayList<String>();
private List<String> descs = new ArrayList<String>();
private List<String> fileNames = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
gvId = (GridView) findViewById(R.id.gv_id);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
});
gvId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View view1 = getLayoutInflater().inflate(R.layout.layout_item01, null);
ImageView imageView = view1.findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(position)));
new AlertDialog.Builder(MainActivity.this).setView(view1).setPositiveButton("确定", null).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
allPic();
}
}
private void allPic() {
names.clear();
descs.clear();
fileNames.clear();
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
// 获取图片的显示名
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
// 获取图片的详细信息
String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
// 获取图片的保存位置的数据
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// 将图片描述添加到集合中
descs.add(desc);
// 将名字添加到集合中
names.add(name);
// 将图片路径添加到fileNames中
fileNames.add(new String(data, 0, data.length - 1));
}
gvId.setAdapter(new MyAdapter());
}
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return fileNames.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 viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item02,parent,false);
viewHolder = new ViewHolder();
viewHolder.imageView = convertView.findViewById(R.id.im_id);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
String s = fileNames.get(position);
Glide.with(MainActivity.this).load(s).into(viewHolder.imageView);
return convertView;
}
private class ViewHolder {
ImageView imageView;
}
}
}