gpt4 book ai didi

java - 将 onclick 设置为 listview/json 时出错

转载 作者:行者123 更新时间:2023-12-01 15:48:21 25 4
gpt4 key购买 nike

当我尝试运行代码时遇到 json 异常。问题发生在 onclick 监听器上。我试图让应用程序在点击时转到 RSS feed 的文章

这是主要 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();
}

}

}
<小时/>

这是适配器:

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 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。检查您如何解析 JSON。这表示您正在尝试获取名为“然后它列出了我想要的网站”的键的值

关于java - 将 onclick 设置为 listview/json 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6659214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com