oussamaniba oussamaniba - 4 months ago 73
JSON Question

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
.

Answer

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.

Comments