Android \ Eclipse:使用动态数组填充ListView

时间:2014-04-18 18:09:34

标签: android android-listview

我在 Android 应用中的活动上有一个 ListView ,然后将由数组ItemDetails中的名称填充

问题是 ListView 似乎没有完全填充,因为在输出中我只有数组的第一个值和这个字符串:

[Ljava.lamg.String;@42974730

活动

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.widget.ListView;

public class ListViewImagesActivity extends Activity {

    private static final String SOAP_ACTION = "http://www.xxx.net/WebService/GetNews";
    private static final String OPERATION_NAME = "GetNews";
    private static final String WSDL_TARGET_NAMESPACE = "http://www.xxx.net/WebServiceNews";
    private static final String SOAP_ADDRESS = "http://www.xxx.net/GetNews.asmx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<ItemDetails> image_details = GetSearchResults();
        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

    }

    private ArrayList<ItemDetails> GetSearchResults() {
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();

        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.dotNet = true;
        envelope.implicitTypes = false;
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        envelope.setOutputSoapObject(request);

        try {

            httpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            String ws = response.toString();
            Log.i("wsString", ws.toString());

            String wsnew = ws.replaceAll("<br />", "\n");
            Log.i("ws", wsnew);

            String[] wss = wsnew.split("\n");
            Log.i("wss0", wss[0].toString());
            Log.i("wss1", wss[1].toString());
            Log.i("wss2", wss[2].toString());

            Spanned spanned = Html.fromHtml(wss.toString());

            ItemDetails item_details = new ItemDetails();
            item_details = new ItemDetails();
            item_details.setName(spanned.toString());
            item_details.setImageNumber(1);
            results.add(item_details);

        } catch (Exception exception) {
            Log.e("Error:  ", exception.toString());
        }

        return results;
    }
}

修改

ItemDetails

public class ItemDetails {

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public int getImageNumber() {
        return imageNumber;
    }

    public void setImageNumber(int imageNumber) {
        this.imageNumber = imageNumber;
    }

    private String name;
    private String itemDescription;
    private String price;
    private int imageNumber;

}

ItemListBaseAdapter

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemListBaseAdapter extends BaseAdapter {
    private static ArrayList<ItemDetails> itemDetailsrrayList;

    private Integer[] imgid = { R.drawable.p1, R.drawable.bb2, R.drawable.p2,
            R.drawable.bb5, R.drawable.bb6, R.drawable.d1 };

    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itemDetailsrrayList.size();
    }

    public Object getItem(int position) {
        return itemDetailsrrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView
                    .findViewById(R.id.name);
            holder.txt_itemDescription = (TextView) convertView
                    .findViewById(R.id.itemDescription);
            holder.txt_itemPrice = (TextView) convertView
                    .findViewById(R.id.price);
            holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);

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

        holder.txt_itemName
                .setText(itemDetailsrrayList.get(position).getName());
        holder.txt_itemDescription.setText(itemDetailsrrayList.get(position)
                .getItemDescription());
        holder.txt_itemPrice.setText(itemDetailsrrayList.get(position)
                .getPrice());
        holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(
                position).getImageNumber() - 1]);

        return convertView;
    }

    static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        TextView txt_itemPrice;
        ImageView itemImage;
    }
}

0 个答案:

没有答案