2011-08-19 47 views
0

如何將我的ListView的每一行中的WebView設置爲相應的url?目前,我有這樣的語句來獲得數據:Android:爲ListView中的每一行設置WebView

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是:

<?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關聯扔。

回答

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

的的ImageManager參考可以在這個post

0

您必須切換到SimpleAdapter並請檢查this previous question and answer

+0

找到我使用的是SimpleAdapter,不是嗎? – Nick

+0

啊,是的,但只有部分方法,將'adapter'的類型改爲SimpleAdapter,然後設置你的'ViewBinder'。 –