从数据库的网格视图中删除照片

时间:2016-09-24 13:44:51

标签: android

我需要删除从json服务器下载并存储到数据库中的照片这是我的代码有什么问题?当我点击照片时会出现一个进度对话框,当我点击它上面的删除按钮时,照片无法删除

public class photos {
private int  id;
private String image;
public photos(int id )
{
    this.id=id;
}
public photos()
{
}
public photos(int id ,String image)
{
    this.id=id;
    this.image=image;
}
public photos(String image) {
    this.setImage(image);
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

}

public class customfunny extends BaseAdapter {
Context c;
List<photos> sites;
public customfunny(Context c, List<photos> sites)
{
    this.c = c;
    this.sites = sites;
}
@Override
public int getCount() {
    return sites.size();
}
@Override
public Object getItem(int i) {
    return sites.get(i);
}
@Override
public long getItemId(int i) {
    return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (view==null)
    {
        view = LayoutInflater.from(c).inflate(R.layout.funnyinflate,viewGroup,false);
    }
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
    photos site = (photos) this.getItem(i);
    Picasso.with(c).load(site.getImage()).into(imageView);
    return view;
}

}

public class PhotosDataBase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "photosDataBase";
// Contacts table name
private static final String TABLE_CONTACTS = "photos";
// Contacts Table Columns names
private static final String KEY_ID = "id";
final String KEY_NAME = "photo";
public PhotosDataBase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT UNIQUE,"
           + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void AddnewPhoto(photos todo)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, todo.getImage());
    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}
public List<photos> getallPhotos() {
    List<photos> contactList = new ArrayList<photos>();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            photos photo = new photos();
            photo.setId(Integer.parseInt(cursor.getString(0)));
            photo.setImage(cursor.getString(1));
            contactList.add(photo);
        } while (cursor.moveToNext());
    }
    return contactList;
}
public void deletePhoto(photos photo) {
    int id = photo.getId();
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID
            + " = " + id, null);
    db.close();
}
public int updateContact(photos photo) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, photo.getImage());
    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[]{String.valueOf(photo.getId())});

}}

public class Funny extends AppCompatActivity {
PhotosDataBase dataBase = new PhotosDataBase(Funny.this);
String url = "http://javawy.fulba.com/yphotos.php";
ProgressDialog dialog;
String pw;
List<photos> list;
photos posts;
customfunny adapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_funny);
    list=  dataBase.getallPhotos();
    final GridView gridView = (GridView) findViewById(R.id.gridview);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject object = new JSONObject(response);
                JSONArray jsonArray = object.getJSONArray("photos");
                 list = new ArrayList<>();
                for (int i = 0;i<jsonArray.length();i++)
                {
                    JSONObject object1 = jsonArray.getJSONObject(i);
                     pw = object1.getString("image");
                     posts = new photos(pw);
                    dataBase.deletePhoto(posts);
                    list.add(posts);
                }
                dialog.dismiss();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    dialog.dismiss();
                    Toast.makeText(Funny.this,"اتصل بالانترنت لتحصل علي احيث الصور الساخره",Toast.LENGTH_LONG).show();
                }
            });
    adapter = new customfunny(Funny.this,list);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(
                    Funny.this);
            adb.setMessage("choose what you need from this photo?");
            adb.setPositiveButton("delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dataBase.deletePhoto(new photos(list.get(i).getId(),posts.getImage()));
                    list.remove(i);
                    adapter.notifyDataSetChanged();
                }
            });
            adb.show();
        }
    });
    dialog = new ProgressDialog(Funny.this);
    dialog.setTitle("downloading");
    dialog.setMessage("جاري تحديث الصور....انتظر");
    dialog.show();
    Volley.newRequestQueue(Funny.this).add(stringRequest);
}

}

0 个答案:

没有答案