2011-07-10 76 views
1

我试图按照本教程http://automateddeveloper.blogspot.com/2011/05/android-rss-reader-20.html 我使用Eclipse和Android的2.0如何点击jsonobject/listview feed项目?

这里发展是*我无法弄清楚如何设置一个onclick监听器或类似的每篇文章 一些问题 *我试图让它去的网站的文章是从的onclick

public class Utezone extends ListActivity { 

private RssListAdapter adapter; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    List<JSONObject> jobs = new ArrayList<JSONObject>(); 
    try { 
     jobs = RssReader.getLatestRssFeed(); 

    } catch (Exception e) { 
     Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString()); 
    } 





    adapter = new RssListAdapter(this,jobs); 
    setListAdapter(adapter); 
} 

}

public class RssListAdapter extends ArrayAdapter<JSONObject> { 

public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) { 
    super(activity, 0, imageAndTexts); 
} 


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

    Activity activity = (Activity) getContext(); 
    LayoutInflater inflater = activity.getLayoutInflater(); 

    // Inflate the views from XML 
    View rowView = inflater.inflate(R.layout.image_text_layout, null); 
    JSONObject jsonImageText = getItem(position); 

    ////////////////////////////////////////////////////////////////////////////////////////////////////// 
    //The next section we update at runtime the text - as provided by the JSON from our REST call 
    //////////////////////////////////////////////////////////////////////////////////////////////////// 
    TextView textView = (TextView) rowView.findViewById(R.id.job_text); 

    try { 
     Spanned text = (Spanned)jsonImageText.get("text"); 
     textView.setText(text); 

    } catch (JSONException e) { 
     textView.setText("JSON Exception"); 
    } 

    return rowView; 

} 


} 


public class RssReader { 

private final static String BOLD_OPEN = "<B>"; 
private final static String BOLD_CLOSE = "</B>"; 
private final static String BREAK = "<BR>"; 
private final static String ITALIC_OPEN = "<I>"; 
private final static String ITALIC_CLOSE = "</I>"; 
private final static String SMALL_OPEN = "<SMALL>"; 
private final static String SMALL_CLOSE = "</SMALL>"; 

/** 
* This method defines a feed URL and then calles our SAX Handler to read the article list 
* from the stream 
* 
* @return List<JSONObject> - suitable for the List View activity 
*/ 
public static List<JSONObject> getLatestRssFeed(){ 
    String feed = "thefeedurl"; 

    RSSHandler rh = new RSSHandler(); 
    List<Article> articles = rh.getLatestArticles(feed); 
    Log.e("RSS ERROR", "Number of articles " + articles.size()); 
    return fillData(articles); 
} 


/** 
* This method takes a list of Article objects and converts them in to the 
* correct JSON format so the info can be processed by our list view 
* 
* @param articles - list<Article> 
* @return List<JSONObject> - suitable for the List View activity 
*/ 
private static List<JSONObject> fillData(List<Article> articles) { 

    List<JSONObject> items = new ArrayList<JSONObject>(); 
    for (Article article : articles) { 
     JSONObject current = new JSONObject(); 
     try { 
      buildJsonObject(article, current); 
     } catch (JSONException e) { 
      Log.e("RSS ERROR", "Error creating JSON Object from RSS feed"); 
     } 
     items.add(current); 
    } 

    return items; 
} 


/** 
* This method takes a single Article Object and converts it in to a single JSON object 
* including some additional HTML formating so they can be displayed nicely 
* 
* @param article 
* @param current 
* @throws JSONException 
*/ 
private static void buildJsonObject(Article article, JSONObject current) throws JSONException { 
    String title = article.getTitle(); 
    String description = article.getDescription(); 
    String date = article.getPubDate(); 

    StringBuffer sb = new StringBuffer(); 
    sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE); 
    sb.append(BREAK); 
    sb.append(description); 
    sb.append(BREAK); 
    sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE); 

    current.put("text", Html.fromHtml(sb.toString())); 
} 
} 

回答

1

由于活动扩展ListActivity,你可以简单地做:

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    //do something 
} 
1

一个onclicklisten呃绑定到一个listview。调用getListView()并将其设置为ListView ivar。在该ivar上,通过调用setOnItemClickListener()方法实现一个OnItemClickListener;

ListView lw = getListView(); 
lw.setOnItemClickListener(new OnItemClickListener({ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} 

}));