2011-09-13 38 views
0

從Web服務獲取我的數據並加載到列表視圖。哪個工作正常。但是當我點擊一個特定的列表項時,我需要將該記錄的id(Id應該來自數據庫)傳遞給另一個活動。這裏是我填充陣列將數據加載到帶有記錄ID的android列表視圖並傳遞確切的記錄ID onListItemClick

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

這是我試着去獲取名稱代碼代碼和IDS

@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

發佈您如何填充視圖的代碼。 –

+0

@YashwanthKumar我更新了代碼。請立即檢查 – virtualpathum

回答

0

據我所知,你沒有問題得到id回來。但是,您可以將標籤提供給任何視圖(這樣您就可以將標籤作爲標籤傳遞給LinearLayout)。然後,如果你有你的身份證,你就可以開始你的新的活動是這樣的:

Intent intent = new Intent(getContext(), YOUR_ACTIVITY_YOU_WANNA_START.class); 
intent.putExtra("ID", userid); 
startActivity(0, intent); 
在recieving活動

(在這種情況下YOUR_ACTIVITY_YOU_WANNA_START.class)你的onCreate也許叫()

Intent intent = getIntent(); 
String userId = intent.getExtra("ID" , "DEFAULT"); 
+0

如何將值設置爲標記?從數據庫中獲取數據時,我嘗試設置textView.setTag(userid)。但它給了我空指針異常。 friendsName =(TextView)findViewById(R.id.txtName); for(int i = 0; i virtualpathum

+0

'你無法在'friendName.setTag(..)中得到NullPointerException'我瘦了getJSONObject返回null或'getString(id)'返回null。你可以用'friendName.setTag(null)'來試試它,以確認它不能成爲'setTag' –

0

在你有一個位置參數的長按事件真的很感謝。無論您在哪裏存儲數據,都可以使用此參數獲取數據。爲獲得進一步的幫助,您應該添加一些關於您使用的適配器類型的信息。

+0

我更新了code.please檢查 – virtualpathum

+0

我認爲位置參數給出了列表視圖中的項目位置。我想將我自己的ID設置到列表項並在點擊事件中返回 – virtualpathum

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

+0

我可以在getNames()裏面設置Person對象的值。但我怎麼能在點擊項目中找回id?請稍微解釋一下。 – virtualpathum

+0

增加了一些:) – richardwiden

+0

請標記解決您的問題的答案/回答您的問題爲「接受」(綠色複選框左側),以便其他人知道什麼解決了問題。 – richardwiden

0

好,這裏是你做什麼,一個新的成員添加到您的holder類,讓我們假設你的ID數據類型爲字符串。然後向持有者添加

public String mId; 

。並在從getView返回之前使用視圖的相關ID填充它。

holder.mId = Id_which_you_need; 

in onItemClickListener,使用它來獲取標籤。

holder = (ViewHolder) v.getTag(); 

並獲得Id,因爲我們認爲它是一個字符串。

id = holder.mId; 

id是你需要的。這就是你需要的一切,還有其他方法,因爲你使用標籤,我認爲這會更好。

1

非常感謝所有給予的支持。我使用@Richard提供的@ rafael-t的標籤概念。感謝@ 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我添加了所有的值,以用戶對象的方法。感謝@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(); 
    } 

我還是覺得這可以進一步優化有更好的表現,但現在它是綽綽有餘爲了我。再次感謝你們。

相關問題