安卓:在ListView设置的WebView的每一行 [英] Android: Setting WebView for each row in a ListView

查看:103
本文介绍了安卓:在ListView设置的WebView的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我哪有我的ListView的每一行中设置的WebView到对应网址是什么?目前,我有这样的语句来获得数据:

How can I set the WebView in each row of my ListView to is corresponding url? Currently I have this statement to get the data:

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

我的列表视图行中的XML是:

The XML for my listview row is:

    <?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关联扔。

From Debugging the exception seems to throw when get to associating the webview with an ID.

推荐答案

我能够得到它变回一个ImageView的和使用本code加载:

I was able to get it to load by changing back to an imageview and using this code:

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

该参考的ImageManager可在此<一个发现href=\"http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview\">post

这篇关于安卓:在ListView设置的WebView的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆