-1

Good Day Guys..Need some help here..i got troubled from deleting the selected row in my list view..i want to use an alert dialog for confirmation to delete the selected row..and to edit also..i am a beginner and i have tried searching for answers to this problem and also tried relating it to other problems but i still didn't get it...

My DatabaseHelper Class


public class DatabaseHelper extends SQLiteOpenHelper implements   Filterable{

// private static final String COLUMN_NAME="ageing_column";
private static final String DATABASE_NAME=" EXPIRATIONMONITORING.DB";
private static final int DATABASE_VERSION = 1;
private static  final String CREATE_QUERY =
        "CREATE TABLE "+ContractClass.NewInfo.TABLE_NAME+"("+ ContractClass.NewInfo.ITEM_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ContractClass.NewInfo.DESCRIPTION+" TEXT, "+
                ContractClass.NewInfo.CATEGORY+" TEXT,"+ ContractClass.NewInfo.MONTHONE+" TEXT, "+ ContractClass.NewInfo.REMIND_AT+" TEXT, "+ ContractClass.NewInfo.QTY+" TEXT, "+
                ContractClass.NewInfo.LOCATION+" TEXT );";
private SQLiteDatabase sqLiteDatabase;

public DatabaseHelper(Context context)
{

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    //this.context1=context;
    Log.e("DATABASE OPERATIONS", "Database created / opened....");
}
@Override
public void onCreate(SQLiteDatabase db) {


    db.execSQL(CREATE_QUERY);

    Log.e("DATABASE OPERATIONS", "Table created....");

}
public void addInformations(String description, String category, String monthOne,String quantity,String remind, String location, SQLiteDatabase db)
{
    ContentValues contentValues=new ContentValues();
    contentValues.put(ContractClass.NewInfo.LOCATION,location);
    contentValues.put(ContractClass.NewInfo.DESCRIPTION,description);
    contentValues.put(ContractClass.NewInfo.CATEGORY,category);
    contentValues.put(ContractClass.NewInfo.MONTHONE,monthOne);
    contentValues.put(ContractClass.NewInfo.REMIND_AT,remind);
    contentValues.put(ContractClass.NewInfo.QTY,quantity);
    db.insert(ContractClass.NewInfo.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATIONS", "One row inserted");
    db.close();
}

public Cursor getInformations(SQLiteDatabase db)
{
    Cursor cursor;
    String[] projections ={ContractClass.NewInfo.DESCRIPTION,ContractClass.NewInfo.CATEGORY,ContractClass.NewInfo.MONTHONE, ContractClass.NewInfo.QTY, ContractClass.NewInfo.REMIND_AT,ContractClass.NewInfo.LOCATION};
    cursor=db.query(ContractClass.NewInfo.TABLE_NAME, projections, null, null, null, null, null);
    return cursor;
}
public Cursor getContact(String location,SQLiteDatabase sqLiteDatabase)
{
    String[] projections ={ContractClass.NewInfo.DESCRIPTION,ContractClass.NewInfo.CATEGORY,ContractClass.NewInfo.MONTHONE, ContractClass.NewInfo.QTY, ContractClass.NewInfo.REMIND_AT,ContractClass.NewInfo.LOCATION};
    String selection = ContractClass.NewInfo.LOCATION+" LIKE? ";
    String [] sargs={location};
    Cursor cursor=sqLiteDatabase.query(ContractClass.NewInfo.TABLE_NAME,projections,selection,sargs,null,null,null);
    return  cursor;
}
public String[] SelectAllData()
{
    try
    {
        String arrData[]=null;
        SQLiteDatabase db;
        db=this.getReadableDatabase();
        String strSQL=" SELECT "+ ContractClass.NewInfo.LOCATION+" FROM "+ ContractClass.NewInfo.TABLE_NAME;
        Cursor cursor =db.rawQuery(strSQL,null);
        if(cursor !=null)
        {
            if(cursor.moveToFirst())
            {
                arrData=new String[cursor.getCount()];
                int i=0;
                do
                {
                    arrData[i]=cursor.getString(0);
                    i++;

                }while(cursor.moveToNext());
            }
        }
        cursor.close();
        return arrData;

    }catch(Exception e){
        return null;
    }
}
public void delete_byID(int id){
    sqLiteDatabase.delete(ContractClass.NewInfo.TABLE_NAME, ContractClass.NewInfo.ITEM_ID + "=" + id, null);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


@Override
public Filter getFilter() {
    return null;
}
}

THis is My MAin Class..


 public class ViewListsActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
 ListDataAdapter  listDataAdapter;
 ListView listView;
 Cursor cursor;
 EditText delete_txt;
 String deletetxt;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_lists_activity);
    listView = (ListView) findViewById(R.id.search_listView);
           listDataAdapter = new   

    ListDataAdapter(getApplicationContext(),R.layout.row_layout);
    databaseHelper = new DatabaseHelper(getApplicationContext());
    databaseHelper = new DatabaseHelper(this);
    delete_txt = (EditText) findViewById(R.id.delete_text);
    sqLiteDatabase = databaseHelper.getReadableDatabase();
    cursor = databaseHelper.getInformations(sqLiteDatabase);
    listView.setAdapter(listDataAdapter);
    if (cursor.moveToFirst()) {
        do {
            String description, category, month1,remind,qty,location;
            description = cursor.getString(0);
            category = cursor.getString(1);
            month1 = cursor.getString(2);
             qty=cursor.getString(3);
            remind=cursor.getString(4);
            location = cursor.getString(5);
            DataProvider dataProvider = new DataProvider(description,  
            category, month1,qty,remind,location);
            listDataAdapter.add(dataProvider);
        } while (cursor.moveToNext());
    }
    listView.setOnItemLongClickListener(new  
   AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View  
    view,  int position, long id) {
            return false;




        }
    });
  }
  public void ToSearchBtn(View view)
  {
    Intent intent=new Intent(this,ThirdActivitySearchAllData.class);
    startActivity(intent);
}
public void ToAddNewItemBtn(View view)
{
    Intent intent=new Intent(this,SecondActivitySaveData.class);
    startActivity(intent);
}

} 

My List Adapter Class


 public class ListDataAdapter extends ArrayAdapter  {
List list = new ArrayList();

public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}



static class LayoutHandler
{
    TextView DESCRIPTION,CATEGORY,MONTH1,QTY,REMIND,LOCATION;
}
public void add(Object object)
{
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row =convertView;
    LayoutHandler layoutHandler;

    if(row==null)
    {
        LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler=new LayoutHandler();

        layoutHandler.DESCRIPTION= (TextView) row.findViewById(R.id.text_description);
        layoutHandler.CATEGORY= (TextView) row.findViewById(R.id.text_category);
        layoutHandler.MONTH1= (TextView) row.findViewById(R.id.text_monthOne);
        layoutHandler.REMIND= (TextView) row.findViewById(R.id.text_remind);
        layoutHandler.QTY= (TextView) row.findViewById(R.id.text_qty);
        layoutHandler.LOCATION= (TextView) row.findViewById(R.id.text_location);
        row.setTag(layoutHandler);
    }else
    {
        layoutHandler=(LayoutHandler)row.getTag();

    }
    DataProvider dataProvider= (DataProvider) this.getItem(position);
    layoutHandler.DESCRIPTION.setText(dataProvider.getDescription());
    layoutHandler.CATEGORY.setText(dataProvider.getCategory());
    layoutHandler.MONTH1.setText(dataProvider.getMonthOne());
    layoutHandler.REMIND.setText(dataProvider.getRemindAt());
    layoutHandler.QTY.setText(dataProvider.getQuantity());
     layoutHandler.LOCATION.setText(dataProvider.getLocation());


    return row;
}


 }
sKhan
  • 9,694
  • 16
  • 55
  • 53

1 Answers1

0

You can use ALERT DIALOG for showing confirmation popup, with YES/NO option both for delete and edit, and based on options selected in ALERT DIALOG you can perform further operations

In the below link you can find the sample code

How do I display an alert dialog on Android?

Community
  • 1
  • 1
Arshad
  • 1,262
  • 16
  • 25
  • thanks for that..but i am looking for the query method for deleting and updating that should be constructed in my Helper Class and how to implement it in my Main Activity onItemClickistener. – peter mora Jan 31 '16 at 07:56
  • i have implemented the AlertDialog.Builder..how about calling the methods of deleting and editing and implementing it.. – peter mora Jan 31 '16 at 08:09