[Solved] If i click on edit button particular row data should be fetch in listview?


With out code I can not help you but I think you do not have the direction or road map . let me show you the road map so that you know how to achieve what you want to do :

  1. implement the click listener on list view. the click should be according to your need such as simple click or a long press or you get set item click listener.

  2. In click listner you can get the index easily and use index to get the item Id from the model class (if you have custom adapter and array )

  3. now you have the index you got the Id , and Id is the key in the database to get the Data from the row. so You have id you can get the data and show that data into the activity where user can edit those values

    this is just the demo code so that you can get the idea , in my case I have this function in Database And I was getting/ retrieving the array list in the following manner

    public ArrayList GetData(String Name) {

      ArrayList<Detail> list = new ArrayList<Detail>();
        cursor = db.rawQuery("select * from MosqueDataNew where Name=""
                + Name + """, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            list.add(new Detail(
                    cursor.getString(cursor.getColumnIndex("ID")),
                    cursor.getString(cursor.getColumnIndex("Name")),
                    cursor.getString(cursor.getColumnIndex("LatitudeLocation")),
                    cursor.getString(cursor.getColumnIndex("LongitudeLocation")),
                    cursor.getString(cursor.getColumnIndex("Size"))));
            return list;
        }
        return null;
    }
    

so this is the way to get the idea to how to retrieve data from the specific row.

  1. Now after getting the editions from the user you can save the values again at the same id so to update the data for this you have to implement the update method in the database. and call that function the Update button click listener.

public boolean UpdateData(ContentValues contentValues, String id) {

if (db.update("YourTableName", contentValues, "ID = '" + id + "'",      null) > 0) {
return true;
}
return false;}

content values are the new updated values you want to save in that place.

Part 2
As You want to edit the values in new activity by clicking on the button of each row , you can achieve it the way if you pass the context to the adapter while creating it, because for opening new activity which you want to use for editing the values, You must have context to start a new activity , this is how you can do it :

public class MyAdapter extends Adapter {
     private Context context;

     public MyAdapter(Context context) {
          this.context = context;     
     }

     public View getView(...){
         View v;
         v.setOnClickListener(new OnClickListener() {
             void onClick() {
                 context.startActivity(...);
             }
         });
     }
}

so as you can see in this you are using to start the activity you wanted to open , so it is clear now how to navigate to new activity , but before doing this you need to get all of the data from database to edit, so this is how you do in button click before opening the new activity

In your getView method of adapter you have to put your click listener which would work for each row , you need to get the id of your item list and then pass it on to database function which is getting all the data for specific Id

editButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //get id of itemlist , or array what ever datamodel you are using  and use it to fetch the data from the database

            }
        });

Edit Part 3

If you want to get the data on basis of id then upper defined function of database would be changed a little bit, here is hows it goes

public ArrayList GetData(String id) {

  ArrayList<Detail> list = new ArrayList<Detail>();
    cursor = db.rawQuery("select * from MosqueDataNew where id = '"
            + ID + "'", null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        list.add(new Detail(
                cursor.getString(cursor.getColumnIndex("ID")),
                cursor.getString(cursor.getColumnIndex("Name")),
                cursor.getString(cursor.getColumnIndex("LatitudeLocation")),
                cursor.getString(cursor.getColumnIndex("LongitudeLocation")),
                cursor.getString(cursor.getColumnIndex("Size"))));
        return list;
    }
    return null;
}

Update part 5

as you have made the method to get all the data from the database now if you need to get the specific row you need to just changed the query

// get userlist
    public List<UserList> getuserlistRow(String id) {
        String selectQuery = "SELECT firstname , lastname"
                + DATABASE_TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * From YOUR_TABLE_NAME where id="'id'"", null);
        List<UserList> userlist = new ArrayList<UserList>();
        if (cursor.moveToFirst()) {
            do {
                UserList list = new UserList();
                list.setId(Integer.parseInt(cursor.getString(0)));
                list.setFirstname(cursor.getString(3));
                list.setLastname(cursor.getString(4));
                userlist.add(list);

            } while (cursor.moveToNext());
        }
        return userlist;

    }

now on button click , you can do this ArrayList myList =
new ArrayList(getuserlistRow(id));
Toast.makText(your_context,””+myList.toString(),Toast.LENGTH_LONG).show():

Note

the above lines and shared code may have syntax error I have just given you the idea ,you can search the syntax on internet easily.

so in this way you are getting all the the data in a complete record , this method is returning data in a array list of of my data type which is “Detail” in my case , you can cast into your data type.

I hope this helps you in understanding how to do that.

31

solved If i click on edit button particular row data should be fetch in listview?