0

I'm having trouble figuring out how to pass information through an Activity switch.
I currently have an Activity where I can add a client to the database, and I have an Activity to view all clients in a ListView.

What I want is that when the user clicks on a client in the ListView for it to go back to the addclient Activity, with all the fields filled in from the database.

here is the Activity where the user views the clients

public class ViewClientActivity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewclient_activity);

    DBHandler handler = new DBHandler(this, null, null, 1);
    SQLiteDatabase db = handler.getWritableDatabase();
    final Cursor ClientCursor = db.rawQuery("SELECT  * FROM clients", null);
    ListView allClients = (ListView) findViewById(R.id.allClients);
    final ClientCursorAdapter clientAdapter = new ClientCursorAdapter(this, ClientCursor);

    allClients.setAdapter(clientAdapter);

    allClients.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            Intent AddClientActivity = new Intent(getApplicationContext(), AddClientActivity.class);
            startActivity(AddClientActivity);
        }
    });

}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shawnzey
  • 196
  • 1
  • 14
  • Please let me know if you need any additional information – Shawnzey May 28 '17 at 21:19
  • There you have an answer. Please let me know if you'd rather want some more explanations on it – droidpl May 28 '17 at 21:33
  • thank you sir ill give that a shot – Shawnzey May 28 '17 at 21:34
  • i do have one more question... Whats the best way to know what listitem was clicked so i can transfer maybe a name or address and SELECT that from the database – Shawnzey May 28 '17 at 21:37
  • You have to use the ViewHolder pattern and on the constructor of the viewHolder set the onClick listener. If you use a recyclerview you can get the adapter position with ```getAdapterPosition``` and if you use a listview you can add a random object into the ```setTag``` of a view. This is a method every view has – droidpl May 28 '17 at 21:38
  • https://stackoverflow.com/questions/6156836/get-selected-item-from-listview-bound-with-simplecursoradapter#6156962 – OneCricketeer May 28 '17 at 21:46

3 Answers3

1

You need to make the client class information implements Parcelable class. This way you will be able to add that information into the Intent you use to open the second Activity. From the second activity, you can get this information from the bundle obtained by calling getIntent()

To add the Parcelable data into de intent use intent.putExtra() and to get it from it use intent.getParcelableExtra()

droidpl
  • 5,872
  • 4
  • 35
  • 47
  • If all the fields are builtins, then its as simple as `intent.getStringExtra(key, default)` for strings :) Check this link for more info https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Praveen Kishore May 28 '17 at 22:01
  • Also, I am just explaining the full object case scenario :) – droidpl May 29 '17 at 17:24
0

when he clicks on a client in the listview for it to go back to the addclient activity but with all the fields filled in from the database

When you "go back", you are doing a SELECT *, so everything will be selected and updated correctly, assuming it was updated in the database.

Your task exists within the AddClientActivity to create a new DBHandler and update your data accordingly.

If you need to pass anything, it would be the ID of the row that was clicked, which you can use setTag within the ClientCursorAdapter class's getView method on the inflated view to put. Then, (Long) view.getTag() to receive in the click.

Also, please use getReadableDatabase() for CursorAdapter

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

This is what i ended up doing to pass the information i needed

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            IMAGES = imagesPath[0] + "|" + imagesPath[1] + "|" + imagesPath[2] + "|" +
                        imagesPath[3] + "|" + imagesPath[4] + "|" + imagesPath[5];

            Intent intent = new Intent(AddClientActivity.this, ImageViewActivity.class);
            intent.putExtra("IMAGES", IMAGES);
            intent.putExtra("POSITION", position);
            startActivity(intent);
        }
    });
Shawnzey
  • 196
  • 1
  • 14