2013-10-26 69 views
0

我是新來的android,我正在實現自定義listview使用數組適配器,在我arraylist我有10個不同的值,但是我設置這個值的列表視圖它只顯示最後一個view.i我很困惑,請清除我的疑問陣列適配器getview只顯示最後一個值

這裏是我的陣列適配器

public class GetAllmessageAdapter extends ArrayAdapter<AllMessageObject>{ 


public List<AllMessageObject> marrayOfList; 
public Context context; 
AllMessageObject messageObject; 
int row; 

public GetAllmessageAdapter(Context context, int textViewResourceId, 
     List<AllMessageObject> arrayOfList) { 
    super(context, textViewResourceId,arrayOfList); 
    this.marrayOfList = arrayOfList; 
    this.context=context; 
    this.row=textViewResourceId; 
} 

public Context getContext() { 
     return context; 
    } 

public int getCount(){ 
    return this.marrayOfList.size(); 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
// return super.getView(position, convertView, parent); 
    System.out.println("in get view"); 
    ViewHolder holder; 

    if(convertView==null){ 

     LayoutInflater Inflator = (LayoutInflater) context 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     convertView=Inflator.inflate(row, null); 

     holder = new ViewHolder(); 

     convertView.setTag(holder); 

    }else 
     holder = (ViewHolder) convertView.getTag(); 

    messageObject=marrayOfList.get(position); 

    holder.user_name = (TextView) convertView.findViewById(R.id.textView1); 
    holder.message = (TextView) convertView.findViewById(R.id.textView2); 
    holder.from_name = (TextView) convertView.findViewById(R.id.textView4); 
    holder.user_img=(ImageView) convertView.findViewById(R.id.imageview1); 

    if(holder.message!=null && messageObject.getMessage().length()>0){ 
     holder.message.setText(messageObject.getMessage()); 
    } 

    if(holder.from_name!=null && messageObject.getFrom_name().length()>0){ 
     holder.from_name.setText(messageObject.getFrom_name()); 
    } 

// holder.user_img.setImageResource(R.drawable.fb); 

    holder.user_name.setText(messageObject.getMessage_date()); 

    return convertView; 
} 

/*private view holder class*/ 
public class ViewHolder { 
    ImageView user_img; 
    TextView user_name; 
    TextView message,from_name; 
} 

這裏是我從我的活動傳遞

protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     System.out.println("background"); 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1); 

     // Use this to add parameters 
     request.addProperty("UserId", 
       "fd83ca17-41f5-472a-8efe-7a975d83ed9b"); 
     request.addProperty("FacebookId", "100006693371290"); 
     request.addPropertyIfValue("count", "6"); 

     // Declare the version of the SOAP request 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     envelope.setOutputSoapObject(request); 
     envelope.dotNet = true; 
     try { 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport.debug = true; 

      // this is the actual part that will call the webservice 

      androidHttpTransport.call(SOAP_ACTION1, envelope); 
      SoapObject soap_result = (SoapObject) envelope.bodyIn; 
      System.out.println("soap_result " + soap_result); 
      result = soap_result.getProperty(0).toString(); 
      System.out.println("result in message activity " + result); 

      JSONArray jsonArray = new JSONArray(result); 
      int a=jsonArray.length(); 
      System.out.println("a "+a); 

      for (int i = 0; i<a ; i++) { 

       JSONObject jsonObject = jsonArray.getJSONObject(i); 

       messageObject = new AllMessageObject(); 

       messageObject.setFrom_name(jsonObject.getString(FROM_NAME)); 
       System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME)); 
       messageObject.setMessage(jsonObject.getString(MESSAGE)); 
       System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE)); 
       messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE)); 
       System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE)); 
      } 

      arrayOfList.add(messageObject); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     System.out.println("on post"); 

     adapter=new GetAllmessageAdapter(Woosuite_Message.this, R.layout.list_row, arrayOfList); 
     listView.setAdapter(adapter); 

    } 

謝謝

+0

看看您所添加的外循環messageObject。這會將最後一個元素單獨添加到arrayOfList。 – N20084753

回答

0

首先,要添加的messageObjectarrayOfList外的for循環。所以,它只會執行一次。試着把放在循環的內部,以獲得所有添加到arraylist的值。

for (int i = 0; i<a ; i++) { 

    JSONObject jsonObject = jsonArray.getJSONObject(i); 

    messageObject = new AllMessageObject(); 

    messageObject.setFrom_name(jsonObject.getString(FROM_NAME)); 
    System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME)); 
    messageObject.setMessage(jsonObject.getString(MESSAGE)); 
    System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE)); 
    messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE)); 
    System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE)); 

    arrayOfList.add(messageObject); 
} 
+0

偉大的nanba ...非常感謝我通過在getview中看到浪費了一個小時...再次感謝 – Gajendran

+0

yes已經我接受了花花公子 – Gajendran

0

請改變你的try塊這樣的,

try { 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport.debug = true; 

      // this is the actual part that will call the webservice 

      androidHttpTransport.call(SOAP_ACTION1, envelope); 
      SoapObject soap_result = (SoapObject) envelope.bodyIn; 
      System.out.println("soap_result " + soap_result); 
      result = soap_result.getProperty(0).toString(); 
      System.out.println("result in message activity " + result); 

      JSONArray jsonArray = new JSONArray(result); 
      int a=jsonArray.length(); 
      System.out.println("a "+a); 

      for (int i = 0; i<a ; i++) { 

       JSONObject jsonObject = jsonArray.getJSONObject(i); 

       messageObject = new AllMessageObject(); 

       messageObject.setFrom_name(jsonObject.getString(FROM_NAME)); 
       System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME)); 
       messageObject.setMessage(jsonObject.getString(MESSAGE)); 
       System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE)); 
       messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE)); 
       System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE)); 
       arrayOfList.add(messageObject); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
+0

感謝您的回覆 – Gajendran

相關問題