简体   繁体   中英

Android: Deleting selected list item

So i have this class called ShowActivity.java. In this class i get a list from a sqlite database. I have added two buttons on this ListView Show Data and Delete Data. So once i select a list Item i would like to perform any one of the operation(Show Data or Delete that selected Row) based on button click event.

In this below code. What i have done is i am selecting a row and once i click on a row item i get a complete toast with an information of that row.

What i want to do is: Once i select a row If I click on show data button i should get a toast of information what i am getting right now. Or

Once i select a row If I click on delete data button i should delete that data from listview as well as database.

public class ShowlistActivity extends Activity {
 String myBtnListener;
private ListView listView;
Button showBtn =findViewById(R.id.show_btn);
Button deleteBtn=findViewById(R.id.delete_btn)

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showlist_ehr);

    listView = (ListView) this.findViewById(R.id.checkout_listview);
    listView.setOnItemClickListener(new ItemClickListener());

    // get the sql string delivered from the QueryActivity
    Intent intent = this.getIntent();
    String sql = intent.getStringExtra("sql");
    // execute the sql
    Cursor cursor = DBOperator.getInstance().execQuery(sql);
    // bind the data to list
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            getApplicationContext(), R.layout.listitem_ehr, cursor,
            new String[] { "PaFirstName", "VisitDate", "MedIssue" }, new 
int[] {
            R.id.stname, R.id.coduedate, R.id.lbtitle },
            SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
    listView.setAdapter(adapter);
    showBtn.setOnClickListener(myBtnListener);
    button2.setOnClickListener(myBtnListener);

    View.OnClickListener myBtnListener= new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.button1_id:
                    //do button 1 action
                    break;
                case R.id.button2_id:
                    //do button 2 action
                    break;
            }
        }
    }
}

private class ItemClickListener implements OnItemClickListener {
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

        Cursor cursor = (Cursor) listView.getItemAtPosition(position);
        String stid = cursor.getString(0);
        String lbtitle = cursor.getString(1);
        String coduedate = cursor.getString(2);
        //String coreturned = cursor.getString(3);
        // cofine = cursor.getString(4);
        //String stname = cursor.getString(5);
        Toast.makeText(getApplicationContext(),"Student ID: " + stid + "\nStudent Name: " + lbtitle+ "\nBook Title: " + coduedate , Toast.LENGTH_LONG).show();



    }
}


}

Add a choice mode to your listview. rounded_border is a custom drawable you make to serve as selected item indicator.

android:choiceMode="singleChoice"
android:listSelector="@drawable/rounded_border"

The update your onCreate

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showlist_ehr);

...

...

    View.OnClickListener myBtnListener= new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(listView.getCheckedItemPosition() >= 0) {
               //get the object
               Cursor selectedCursor = (Cursor) listView.getItemAtPosition(listView.getCheckedItemPosition());

               switch(view.getId()){
                   case R.id.show_btn:
                       //show selectedCursor details
                       break;
                   case R.id.delete_btn:
                       String stid = selectedCursor.getString(0);
                       //change the stud_id to your database field id name
                       String sql = "DELETE FROM Student WHERE stud_id = '" + stid + "'";
                       Cursor cursor = DBOperator.getInstance().execQuery(sql);
                       adapter.changeCursor(cursor);
                       break;
               }
           }
        }
    }

    showBtn.setOnClickListener(myBtnListener);
    deleteBtn.setOnClickListener(myBtnListener);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM