Android:为ListView中的每一行设置WebView

时间:2011-08-19 18:43:53

标签: android listview webview

如何将ListView的每一行中的WebView设置为相应的url?目前我有这个声明来获取数据:

    public void getTweets(String selection) {



    String formatedcat = selection.toLowerCase();
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    JSONObject json = JSONfunctions
            .getJSONfromURL("http://mysite.com/twitter.php);

    try {

        JSONArray category = json.getJSONArray(formatedcat);
        for (int i = 0; i < category.length(); i++) {



            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject c = category.getJSONObject(i);

            map.put("id", String.valueOf(i));
            String url = c.getString("image");
            map.put("name",
                    c.getString("fullName") + "\n(#" + c.getString("name")
                            + ") ");
            map.put("text",c.getString("text"));
            map.put("timestamp", c.getString("timestamp"));
            mylist.add(map);

            WebView mWebView;
            mWebView = (WebView) findViewById(R.id.lvicon);
            //mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl(url);


        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.list,
            new String[] { "name", "text", "timestamp"}, new int[] { R.id.item_title,
                    R.id.item_subtitle, R.id.timestamp});
    setListAdapter(adapter);



    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    m_ProgressDialog.dismiss();
}

我的listview行的XML是:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="7dp">
    <WebView android:id="@+id/lvicon" android:layout_width="52dp"
        android:layout_height="52dp" android:layout_marginRight="6dip"
        />
    <TextView android:id="@+id/item_title" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:textColor="#010002" android:textStyle="bold"
        android:padding="2dp" android:textSize="18dp" android:layout_toRightOf="@+id/lvicon" />
    <TextView android:id="@+id/item_subtitle" android:textColor="#010002"   android:autoLink="web"
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/item_title"
        android:padding="2dp" android:textSize="13dp" />
    <TextView android:id="@+id/timestamp" android:textColor="#010002" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/item_subtitle"
        android:padding="1dp" android:textSize="10dp" android:gravity="right"/>

</RelativeLayout>

从调试开始,当将webview与ID相关联时,似乎抛出异常。

2 个答案:

答案 0 :(得分:0)

您必须切换到SimpleAdapter,请检查this previous question and answer

答案 1 :(得分:0)

我可以通过更改回imageview并使用以下代码来加载它:

    public class TweetAdapter extends SimpleAdapter {

    public ImageManager imageManager;
    public ListActivity context;
    public ArrayList<HashMap<String,String>> list;
    public String[] fieldNames;
    public int[] fieldTargetIds;

    public TweetAdapter(ListActivity c, 
            ArrayList<HashMap<String, String>> mylist,
            int textViewResourceId,
            String[] fieldNames,
            int[] fieldTargetIds) {
        super(c, mylist, textViewResourceId, fieldNames, fieldTargetIds );
        this.context = c;
        this.list = mylist;
        this.fieldNames = fieldNames;
        this.fieldTargetIds = fieldTargetIds;
        this.imageManager = new ImageManager(context.getApplicationContext());
    }

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

        View row = convertView;
        if (row == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = vi.inflate(R.layout.list, null);
        }
        //super.getView(position, convertView, parent);
        ImageView imgView = (ImageView) row.findViewById(R.id.lvicon);

        try {
            String url = list.get(position).get("image");
            imgView.setTag(url);
            imageManager.displayImage(url, context, imgView);
        } catch (Exception e) {

        }

        for (int i=0; i<fieldNames.length; i++) {
            TextView tv = (TextView) row.findViewById(fieldTargetIds[i]);
            tv.setText(list.get(position).get(fieldNames[i]));              
        }


        return row;
    }
}

可以在此post

找到imagemanager参考