Skip to content Skip to sidebar Skip to footer

Passing Data From Listview To Edittext Of Another Activity

package com.supdeco.oussamaniba.loginapp; import android.content.ClipData; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingAc

Solution 1:

Do this way,

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            publicvoidonItemClick(AdapterView<?> arg0, View arg1, int position, long id)
            {
                Intent intent = new Intent(getApplicationContext(), SingleUser.class);
                intent.putExtra("username", YourModels.get(position).getUsername());//here first get position and than pass data you want to pass
                intent.putExtra("fk_Code", "" + YourModels.get(position).getFk_Code());//take data from your model
                startActivity(intent);
            }
        });

Check this link for more information.

Solution 2:

It was too simple i found a simple solution i created a bunch of string arrays that contains each of the data fetched from the DB and stored in them, so now i can choose from those String arrays by position, but thanks anyway

package com.supdeco.oussamaniba.loginapp;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

publicclassDisplayListViewextendsAppCompatActivity {

    String JSON_STRING;
    JSONObject jsonObject;
    JSONArray jsonArray;
    ContactAdapter contactAdapter;
    ListView listView;
    TextView lstv;
    String username,email,password,name,last;


    List<String> susername = newArrayList<String>();
    List<String> sname = newArrayList<String>();
    List<String> slname = newArrayList<String>();
    List<String> spassword = newArrayList<String>();
    List<String> semail = newArrayList<String>();

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);


        listView = (ListView) findViewById(R.id.list);
        lstv = (TextView) findViewById(R.id.lstv);

        contactAdapter = newContactAdapter(this, R.layout.row_layout);
        listView.setAdapter(contactAdapter);

        JSON_STRING = getIntent().getExtras().getString("json_data");


        try {
            jsonObject = newJSONObject(JSON_STRING);
            jsonArray = jsonObject.getJSONArray("server_response");


            intcount=0;


            while(count<jsonArray.length()){

                JSONObjectJO= jsonArray.getJSONObject(count);
                username = JO.getString("username");
                email = JO.getString("email");
                password = JO.getString("password");
                name = JO.getString("name");
                last = JO.getString("lastname");

                Contactscontacts=newContacts(username,email,password,name,last);
                contactAdapter.add(contacts);

                count++;

                susername.add(username);
                sname.add(name);
                slname.add(last);
                spassword.add(password);
                semail.add(email);

                lstv.setText("Available: " + count + " members");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
            @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int position, long id)
            {
                Intentintent=newIntent(getApplicationContext(), SingleUser.class);

                String[] N = newString[sname.size()];
                N = sname.toArray(N);

                String[] L = newString[slname.size()];
                L = slname.toArray(L);

                String[] U = newString[susername.size()];
                U = susername.toArray(U);

                String[] P = newString[spassword.size()];
                P = spassword.toArray(P);

                String[] E = newString[semail.size()];
                E = semail.toArray(E);

                intent.putExtra("name", N[position]);
                intent.putExtra("last", L[position]);
                intent.putExtra("username", U[position]);
                intent.putExtra("password", P[position]);
                intent.putExtra("email", E[position]);
                startActivity(intent);
            }
        });
    }
}

Post a Comment for "Passing Data From Listview To Edittext Of Another Activity"