列表视图适配器中的图像

时间:2011-11-22 07:55:23

标签: android listview android-arrayadapter custom-adapter

我正在尝试通过扩展ArrayList

来创建自定义列表视图适配器

当我执行以下操作时,我无法从URL获取图像并将其显示在列表视图中,但它显示XML中的默认图像

Item.java

public class Item {

    String itemTitle = "", itemTimestamp = "", itemDescription = "", itemImage ="";
    public Item(String itemTitle, String itemTimestamp, String itemDescription,
            String itemImage) {
        this.itemTitle = itemTitle;
        this.itemDescription = itemDescription;
        this.itemTimestamp = itemTimestamp;
        this.itemImage = itemImage;
    }

    public String getItemTitle() {
        return itemTitle;
    }

    public void setItemTitle(String itemTitle) {
        this.itemTitle = itemTitle;
    }

    public String getItemTimestamp() {
        return itemTimestamp;
    }

    public void setItemTimestamp(String itemTimestamp) {
        this.itemTimestamp = itemTimestamp;
    }

    public String getItemDescription() {
        return itemDescription;
    }

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

    public String getItemImage() {
        return itemImage;
    }

    public void setItemImage(String itemImage) {
        this.itemImage = itemImage;
    }

}

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Item> {

    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    private List<Item> items;

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
        this.items = items;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_item_default, null);
        }

        Item p = items.get(position);

        if (p != null) {

            TextView list_title = (TextView) v.findViewById(R.id.list_title);
            TextView list_description = (TextView) v
                    .findViewById(R.id.list_description);
            TextView list_timestamp = (TextView) v
                    .findViewById(R.id.list_timestamp);
            ImageView list_image = (ImageView) v.findViewById(R.id.list_image);

            if (list_title != null) {
                list_title.setText(p.getItemTitle());
            }

            if (list_description != null) {
                list_description.setText(p.getItemDescription());
            }

            if (list_timestamp != null) {
                list_timestamp.setText(p.getItemTimestamp());
            }

            if (list_image != null) {
                Uri imageURI = Uri.parse(p.getItemImage());
                list_image.setImageURI(imageURI);
            }
        }

        return v;
    }

}

MessageActivity.java

public class MessagesActivity extends Activity {

    ListView listview;
    static ArrayList<Item> dataArray = new ArrayList<Item>();
    static ArrayList<Item> contentArray = new ArrayList<Item>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);

        setupViews();
        try {
            contentArray = generateArray(createJson());
            Log.d("bMobile", createJson());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.d("bMobile", "start ListAdapter");
        listview.setAdapter(new ListAdapter(MessagesActivity.this, R.layout.list_item_default,
                contentArray));
        Log.d("bMobile", "end ListAdapter");
    }

    public void setupViews() {

        listview = (ListView) findViewById(R.id.list_items);

        ((TextView) findViewById(R.id.title_text))
                .setText(R.string.description_messages);
    }

    // It creates a JSON and returns JSON string
    public String createJson() throws JSONException {
        JSONArray itemArray = new JSONArray();

        JSONObject itemObject1 = new JSONObject();
        itemObject1.put("title", "Harsha MV");
        itemObject1.put("timestamp", "2 hours");
        itemObject1.put("description", "Bangalore, India");
        itemObject1.put("display_photo", "http://i.imgur.com/enUZr.jpg");

        JSONObject itemObject2 = new JSONObject();
        itemObject2.put("title", "Avinash G");
        itemObject2.put("timestamp", "4 days");
        itemObject2.put("description", "Mysore, India");
        itemObject2.put("display_photo",
                "http://imgn.dt07.net/1077/1077092_b.jpg");

        JSONObject itemObject3 = new JSONObject();
        itemObject3.put("title", "Jyosna Sahoo");
        itemObject3.put("timestamp", "1  year");
        itemObject3.put("description", "Rourkela, India");
        itemObject3.put("display_photo",
                "http://imgn.dt07.net/1099/1099091_b.jpg");

        itemArray.put(itemObject1);
        itemArray.put(itemObject2);
        itemArray.put(itemObject3);

        return itemArray.toString();
    }

    public ArrayList<Item> generateArray(String JSONdata) throws JSONException {

        JSONArray listData = new JSONArray(JSONdata);
        for (int i = 0; i < listData.length(); i++) {

            JSONObject listObject = listData.getJSONObject(i);
            String item_title = listObject.getString("title");
            String item_timestamp = listObject.getString("timestamp");
            String item_description = listObject.getString("description");
            String item_image = listObject.getString("display_photo");

            Item ObjectItem = new Item(item_title, item_description,
                    item_timestamp, item_image); 
            dataArray.add(ObjectItem);

        }
        return dataArray;
    }

}

activity_messages.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="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/title_container"
        style="@style/TitleBar" >

        <ImageButton
            style="@style/TitleBarAction"
            android:contentDescription="@string/description_home"
            android:onClick="onHomeClick"
            android:src="@drawable/ic_title_home" />

        <ImageView style="@style/TitleBarSeparator" />

        <TextView
            android:id="@+id/title_text"
            style="@style/TitleBarText" />

        <ImageView style="@style/TitleBarSeparator" />

        <ImageButton
            android:id="@+id/btn_title_refresh"
            style="@style/TitleBarAction"
            android:contentDescription="@string/description_refresh"
            android:onClick="onRefreshClick"
            android:src="@drawable/ic_title_refresh" />

        <ProgressBar
            android:id="@+id/title_refresh_progress"
            style="@style/TitleBarProgressIndicator"
            android:visibility="gone" />
    </LinearLayout>

    <ListView
        android:id="@+id/list_items"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/list_empty_visitors" />

</LinearLayout>

list_item_default.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="64dip"
            android:layout_height="64dip"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="8dip"
            android:background="@drawable/profile" >
        </ImageView>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/list_image"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/relativeLayout2"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent" >

                <TextView
                    android:id="@+id/list_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_toLeftOf="@+id/list_timestamp"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="Harsha Mallikarjun Vantagudi"
                    android:textSize="@dimen/text_size_medium"
                    android:textStyle="bold" >
                </TextView>

                <TextView
                    android:id="@+id/list_timestamp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:padding="6dip"
                    android:paddingLeft="30dip"
                    android:singleLine="true"
                    android:text="2 hours"
                    android:textSize="@dimen/text_size_xxsmall" >
                </TextView>
            </RelativeLayout>

            <TextView
                android:id="@+id/list_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Bangalore, India Bangalore, India Bangalore, India Bangalore, India"
                android:textSize="@dimen/text_size_small" >
            </TextView>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

generateArray(...)返回null,您将contentArray设置为然后传递给构造函数方法。您正在填充dataArray,然后从不使用它。