使用Blogger API,我在列表视图中显示我博客的热门文章标题。在JSON响应中,我还获取相应帖子的URL。我想要做的是将相应的URL添加到标题中,这样当用户点击标题时,链接的帖子就会在浏览器中打开。 我怎样才能做到这一点?
我的MainActivity.java
公共类MainActivity扩展了AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
String baseUrl = "https://www.googleapis.com/blogger/v2/blogs/6648452808939065157/posts";
String apiKey = "?key=" + BuildConfig.API_KEY;
String blogUrl = (baseUrl.concat(apiKey));
//ArrayList<HashMap<String, String>> contactList;
ArrayList<HashMap<String, String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//contactList = new ArrayList<>();
itemList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetItems().execute();
}
public void item_click(View view) {
startActivity(new Intent(this, ContentActivity.class));
}
/**
* Async task class to get json by making HTTP call
*/
private class GetItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(blogUrl);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray items = jsonObj.getJSONArray("items");
// looping through Latest Articles
for (int i = 0; i < 1; i++) {
JSONObject c = items.getJSONObject(i);
String title = c.getString("title");
String post_url = c.getString("url");
HashMap<String, String> item = new HashMap<>();
// adding each child node to HashMap key => value
item.put("title", title);
item.put("url",post_url);
// adding item to article list
itemList.add(item);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this,
itemList,R.layout.list_item, new String[]{"title"},
new int[]{R.id.title});
lv.setAdapter(adapter);
}
}
}
答案 0 :(得分:0)
您应该在ListView适配器的标题上添加OnClickListener
。通过设置yourTextView.setClickable(true);
后跟yourTextView.setOnClickListener(clickListener);
。
我看到您将整个hashedMap itemList
已解析的JSON数据传递给SimpleAdapter
构造函数。在您的SimpleAdapter
OnClickListener
内,您可以使用WebView
并通过从HashMap中检索来显示该网址。您可以使用getAdapterPosition()
获取点击的排名。使用SimpleAdapter代码更新帖子也会有所帮助。