1
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.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • 1
    Hi and welcome to SO. Read [how to ask](http://stackoverflow.com/help/how-to-ask) and [mcve](http://stackoverflow.com/help/mcve) for asking a better received question. – davejal Jan 11 '16 at 00:29
  • You haven't actually updated the ListView by calling notifyDataSetChanged() – Keshav Jan 11 '16 at 00:46
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Yaw Asare Jan 11 '16 at 04:02
  • Keshav how to do it ? – oussamaniba Jan 11 '16 at 11:28

2 Answers2

0

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.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

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