简体   繁体   中英

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.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class DisplayListView extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);


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

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

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


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


            int count = 0;


            while(count<jsonArray.length()){

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

                Contacts contacts = new Contacts(username,email,password,name,last);
                contactAdapter.add(contacts);

                count++;

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


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
            {
                Intent intent = new Intent(getApplicationContext(), SingleUser.class);
                intent.putExtra("username", String.valueOf(listView.getSelectedItem()));
                startActivity(intent);
            }
        });
    }
}

I try to pass data from this ListView to another EditText in an other activity but the result is always null , I want to pass all the text string from the ListView to the EditText .

Do this way,

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(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.

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;

public class DisplayListView extends AppCompatActivity {

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


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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);


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

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

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


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


            int count = 0;


            while(count<jsonArray.length()){

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

                Contacts contacts = new Contacts(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(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
            {
                Intent intent = new Intent(getApplicationContext(), SingleUser.class);

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

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

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

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

                String[] E = new String[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);
            }
        });
    }
}

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