我从Web服务获取数据并加载到列表视图。哪个工作正常。但是当我点击一个特定的列表项时,我需要将该记录的ID(Id应该来自数据库)传递给另一个活动。这是我填充数组的代码
public String[] getNames(String response){
String[] friends = null;
try {
JSONObject json = new JSONObject(response);
String values = json.getString("friends");
JSONArray jsonArray = new JSONArray(values);
friends = new String[jsonArray.length()];
//Bundle b = new Bundle();
for (int i = 0; i < jsonArray.length(); i++) {
friends[i] = jsonArray.getJSONObject(i).getString("fname")+ " " + jsonArray.getJSONObject(i).getString("lname") ;
friends[i+1] = jsonArray.getJSONObject(i).getString("id");
//i++;
//friends[i]= jsonArray.getJSONObject(i).getString("id");
}
} catch (Exception e) {
e.printStackTrace();
}
return friends;
}
这是我试图获取名称和ID的代码
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String name = o.toString();
Object x = this.getListAdapter().getItem(position+1);
String userid= x.toString();
Toast.makeText(this, "You selected: " + name +"The Id: "+userid, Toast.LENGTH_LONG)
.show();
}
这是我的列表视图的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
>
<ImageView
android:id="@+id/icon"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/man"
/>
<TextView
android:id="@+id/txtName"
android:layout_width="210px"
android:layout_height="60px"
android:padding="4dp"
android:textSize="12sp"
android:textColor="#000000"
android:background="#ffffff"
/>
</LinearLayout>
这就是我填充列表视图的方式
public void setTheList(String response){
String friends[] = getNames(response);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
if(adapter==null){
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
}
adapter = new MyArrayAdapter(this, friends);
this.setListAdapter(adapter);
}
}
这是我的适配器。我是从here
得到的package virtualpathum.web;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
public MyArrayAdapter(Activity context, String[] names) {
super(context, R.layout.friend, names);
this.context = context;
this.names = names;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView;
public ImageButton ibConfirm;
public ImageButton ibNotNow;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.friend, null, true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.txtName);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
//holder.ibConfirm = (ImageButton) rowView.findViewById(R.id.ibNotNow);
//holder.ibNotNow= (ImageButton) rowView.findViewById(R.id.ibNotNow);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText(names[position]);
// Change the icon for Windows and iPhone
String s = names[position];
holder.imageView.setImageResource(R.drawable.man);
return rowView;
}
}
如果您对此有所了解,真的很感激 谢谢 巴吞
答案 0 :(得分:1)
而是getnames返回String []创建一个表示一个人的类以及一个id并返回ArrayList
class Person {
String name;
int id;
public Person(int id, String name){
//Implement
}
//Implement getters and setters
}
//Consider renaming to getPersons
ArrayList<Person> person = getNames();
getNames(String inputString)
{
...
ArrayList<Person> persons = new ArrayList...
persons.add(new Person("Brother",2)
persons.add(new Person("Jonas",1)
return persons;
}
ArrayAdapter<Person> arrayadapter = new YourArrayAdapter<Person>();
arrayadapter.addAll(getNames(inputString);
In you arrayadapter you override the getItemId
@Overide
getItemId(int position)
{
return getItem(position).getItemId();
}
(我假设您正在使用ArrayAdapter或类似的东西)
然后在你的arrayadaper中相应地实现getId和getItem
答案 1 :(得分:1)
非常感谢所有给予的支持。我使用@Richard提供的方法@ rafael-t的Tag概念。谢谢@ yashwanth-kumar的解释。以下是我原始代码的修改
public ArrayList<Object> getNames(String response){
ArrayList<Object> arrList = null;
try {
JSONObject json = new JSONObject(response);
String values = json.getString("friends");
JSONArray jsonArray = new JSONArray(values);
arrList = new ArrayList<Object>();
for (int i = 0; i < jsonArray.length(); i++) {
arrList.add(new User(jsonArray.getJSONObject(i).getString("id"),jsonArray.getJSONObject(i).getString("fname"),jsonArray.getJSONObject(i).getString("lname"),jsonArray.getJSONObject(i).getString("email")));
}
} catch (Exception e) {
e.printStackTrace();
}
return arrList;
}
这是我的getNames方法我将所有值添加到User Object。感谢@Richard。
以下是此次更改的自定义适配器
package virtualpathum.web;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MyArrayAdapter extends ArrayAdapter<ArrayList<Object>> {
private final Activity context;
private final ArrayList names;
@SuppressWarnings("unchecked")
public MyArrayAdapter(Activity context, ArrayList names) {
super(context, R.layout.friend, names);
this.context = context;
this.names = names;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView;
public ImageButton ibConfirm;
public ImageButton ibNotNow;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.friend, null, true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.txtName);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
//holder.ibConfirm = (ImageButton) rowView.findViewById(R.id.ibNotNow);
//holder.ibNotNow= (ImageButton) rowView.findViewById(R.id.ibNotNow);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
String [] frndNames = new String[names.size()];
int count = 0;
for (@SuppressWarnings("rawtypes")
Iterator iterator = names.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
frndNames[count] = user.getFirstname()+" "+user.getLastname();
count++;
}
String [] frndIds = new String[names.size()];
int idCount = 0;
for (@SuppressWarnings("rawtypes")
Iterator iterator = names.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
frndIds[idCount] = user.getUserid();
idCount++;
}
holder.textView.setText(frndNames[position]);
holder.textView.setTag(frndIds[position]);
holder.imageView.setImageResource(R.drawable.man);
return rowView;
}
}
这是单击侦听器
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
String userid = (String) v.findViewById(R.id.txtName).getTag();
Toast.makeText(this, "Selected user ID = "+userid , Toast.LENGTH_LONG)
.show();
}
我仍然觉得这可以进一步优化以获得更好的性能,但现在对我来说已经绰绰有余了。再次谢谢你们。
答案 2 :(得分:0)
在长按事件中,您有一个位置参数。无论您在何处存储数据,都可以使用此参数获取数据。如需进一步的帮助,您可能应该添加一些有关您使用的适配器类型的信息。
答案 3 :(得分:0)
据我所知,你没有问题可以恢复身份。但是,您可以为任何视图提供标记(因此您可以将ID作为Tag传递给LinearLayout)。然后,如果您有自己的身份证,就可以开始新的活动:
Intent intent = new Intent(getContext(), YOUR_ACTIVITY_YOU_WANNA_START.class);
intent.putExtra("ID", userid);
startActivity(0, intent);
在接收活动(在本例中为YOUR_ACTIVITY_YOU_WANNA_START.class)中,您可以在onCreate()中调用
Intent intent = getIntent();
String userId = intent.getExtra("ID" , "DEFAULT");
答案 4 :(得分:0)
好的,这就是你要做的,在你的持有者类中添加一个新成员,让我们假设你的id数据类型是字符串。然后添加
public String mId;
持有人。并在从getView返回之前使用视图的相关id填充它。
holder.mId = Id_which_you_need;
在onItemClickListener中,使用它来获取标记。
holder = (ViewHolder) v.getTag();
并获取Id,因为我们假设它是一个字符串。
id = holder.mId;
id就是你所需要的。这就是你所需要的,还有其他方法,因为你使用的是标签,我认为这会更好。