0

我是 android 新手,我正在使用数组适配器实现自定义列表视图,在我的数组列表中我有 10 个不同的值,但是当我将此值设置为列表视图时,它只显示最后一个视图。我很困惑,请清除我的疑问

这是我的阵列适配器

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

    }

谢谢

4

2 回答 2

0

首先,您将messageObject添加到for 循环之外的arrayOfList 。所以,它只会执行一次。尝试将该行放在 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);
 }
于 2013-10-26T05:47:28.583 回答
0

请像这样更改您的尝试块,

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();
        }
于 2013-10-26T05:50:03.410 回答