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);
}
});
}
}
我尝试将数据从此ListView传递到另一个的另一个EditText 活动,但结果总是 null ,我想传递所有文字 从ListView到EditText的字符串。
答案 0 :(得分:0)
这样做,
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);
}
});
查看此link以获取更多信息。
答案 1 :(得分:0)
这太简单了我找到了一个简单的解决方案,我创造了一堆 包含从DB获取的每个数据的字符串数组 存储在它们中,所以现在我可以从那些String数组中选择 位置,但无论如何,谢谢
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);
}
});
}
}