开发者

error when setting an onclick to a listview/json

I'm getting a json exception when I try to run my code. The problem happens on the onclick listener. I'm trying to have the application go to the article of the rss feed on click

Here is the main activity

public class Blocku 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 = BlockuReader.getLatestRssFeed();
    } catch (Exception e) {
        Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
    }

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

protected void onListItemClick(ListView l, View v, int position, long id) {
       super.onListItemClick(l, v, position, id);

       String link = null;
    try {
        String url = Article.getUrl().toString();

        link = adapter.getItem(position).getString(url).toString();
         Intent i = new Intent(Intent.ACTION_VIEW);
           i.setData(Uri.parse(link));
           startActivity(i);
    } catch (JSONException e) {
        Context context = getApplicationContext();
        CharSequence text = "error";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        e.printStackTrace();
    }

    }

}

here is the 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");
    开发者_JAVA百科}

    return rowView;

} 


}

and the article class

public class Article {

private long articleId;
private long feedId;
private String title;
private String description;
private String pubDate;
private static URL url;
private String encodedContent;

private static String link;
/**
 * @return the articleId
 */
public long getArticleId() {
    return articleId;
}
/**
 * @param articleId the articleId to set
 */
public void setArticleId(long articleId) {
    this.articleId = articleId;
}
/**
 * @return the feedId
 */
public long getFeedId() {
    return feedId;
}
/**
 * @param feedId the feedId to set
 */
public void setFeedId(long feedId) {
    this.feedId = feedId;
}
/**
 * @return the title
 */
public String getTitle() {
    return title;
}
/**
 * @param title the title to set
 */
public void setTitle(String title) {
    this.title = title;
}
/**
 * @return the url
 */
public static  URL getUrl() {
    return url;
}
/**
 * @param url the url to set
 */
public void setUrl(URL url) {
    Article.url = url;
}
/**
 * @param description the description to set
 */
public void setDescription(String description) {
    this.description = description;
}
/**
 * @return the description
 */
public String getDescription() {
    return description;
}
/**
 * @param pubDate the pubDate to set
 */
public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
}
/**
 * @return the pubDate
 */
public String getPubDate() {
    return pubDate;
}
/**
 * @param encodedContent the encodedContent to set
 */
public void setEncodedContent(String encodedContent) {
    this.encodedContent = encodedContent;
}
/**
 * @return the encodedContent
 */
public String getEncodedContent() {
    return encodedContent;
}


}


JSONException is usually thrown when there are issues with parsing. Check how you are parsing the JSON. this says that you are trying to get a value for a key named "then it lists the site i want"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜