English 中文(简体)
图一
原标题:Image in List View Adaptor

我试图通过扩大阿雷拉派教徒的范围,形成一个习俗清单。

当我做以下工作时,我无法从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;
    }

}

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

}

active_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>
最佳回答

generateArray(...) returns null, which you re setting contentArray to and then passing into your constructor method. You re populating dataArray then never using it.

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签