Android: Deleting selected list item












0














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();



}
}


}









share|improve this question
























  • Possible duplicate of Setting click listener on button in Listview row layout
    – Jantzilla
    Nov 21 at 2:32










  • I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
    – Nick
    Nov 21 at 3:15
















0














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();



}
}


}









share|improve this question
























  • Possible duplicate of Setting click listener on button in Listview row layout
    – Jantzilla
    Nov 21 at 2:32










  • I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
    – Nick
    Nov 21 at 3:15














0












0








0







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();



}
}


}









share|improve this question















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();



}
}


}






java android sqlite android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 3:38

























asked Nov 21 at 2:06









Nick

95




95












  • Possible duplicate of Setting click listener on button in Listview row layout
    – Jantzilla
    Nov 21 at 2:32










  • I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
    – Nick
    Nov 21 at 3:15


















  • Possible duplicate of Setting click listener on button in Listview row layout
    – Jantzilla
    Nov 21 at 2:32










  • I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
    – Nick
    Nov 21 at 3:15
















Possible duplicate of Setting click listener on button in Listview row layout
– Jantzilla
Nov 21 at 2:32




Possible duplicate of Setting click listener on button in Listview row layout
– Jantzilla
Nov 21 at 2:32












I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
– Nick
Nov 21 at 3:15




I have seen this solution. Does not fit in my question. I want to show a toast when row is selected and show button is clicked. And other thing is i want to delete a selcted row on delete button click event.
– Nick
Nov 21 at 3:15












1 Answer
1






active

oldest

votes


















1














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);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404347%2fandroid-deleting-selected-list-item%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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);
    }





    share|improve this answer




























      1














      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);
      }





      share|improve this answer


























        1












        1








        1






        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);
        }





        share|improve this answer














        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);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 8:37

























        answered Nov 21 at 2:51









        Android_K.Doe

        655118




        655118






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404347%2fandroid-deleting-selected-list-item%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'