我有一个json数组,我使用for
循环遍历每个对象以获取作者名称。如果作者姓名等于我输入的作者姓名,则该名称将添加到arraylist中。然后使用阵列适配器调整该阵列列表。
但这是我的问题 - 我正在使用这种方法搜索json数组来查找匹配项。
private void updateListView() {
ArrayList<String> arrayList = new ArrayList<String>();
try {
JSONArray jsonArray = mBlogData.getJSONArray("posts");
for (int i = 0 ; i <jsonArray.length() ; i++ ) {
JSONObject post = jsonArray.getJSONObject(i);
String stringPost = post.getString(KEY_AUTHOR);
Log.v(TAG , stringPost);
if (stringPost.equals("Guil Hernandez")) {
arrayList.add(stringPost);
} else {
Log.v(TAG , "no match " + stringPost);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this , android.R.layout.simple_list_item_1,
arrayList);
setListAdapter(adapter);
所以现在我有我的过滤数据....我想对每个结果都有意图。所以我这样做
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
JSONArray jsonArrayy = null;
try {
jsonArrayy = mBlogData.getJSONArray("posts");
JSONObject jPost = jsonArrayy.getJSONObject(position);
String url = jPost.getString("url");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
但因为数据被过滤掉了......它打开了错误的网址!我不知道如何设置我的意图,因此它将获取过滤数据的URL ...请帮助。如果这是令人困惑的完整代码和JSON数据将在下面列出并随时问我任何问题!谢谢。
public class MyActivity extends ListActivity {
public final static String TAG = MyActivity.class.getSimpleName();
protected JSONObject mBlogData;
private final String KEY_AUTHOR = "author";
private final String KEY_TITLE = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (networkIsAvailable()) {
GetNameData getNameData = new GetNameData();
getNameData.execute();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
JSONArray jsonArrayy = null;
try {
jsonArrayy = mBlogData.getJSONArray("posts");
JSONObject jPost = jsonArrayy.getJSONObject(position);
String url = jPost.getString("url");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
private boolean networkIsAvailable() {
boolean isOn = false;
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if ( networkInfo != null && networkInfo.isConnected()) {
isOn = true;
}
return isOn;
}
private void updateListView() {
ArrayList<String> arrayList = new ArrayList<String>();
try {
JSONArray jsonArray = mBlogData.getJSONArray("posts");
for (int i = 0 ; i <jsonArray.length() ; i++ ) {
JSONObject post = jsonArray.getJSONObject(i);
String stringPost = post.getString(KEY_AUTHOR);
Log.v(TAG , stringPost);
if (stringPost.equals("Guil Hernandez")) {
arrayList.add(stringPost);
} else {
Log.v(TAG , "no match " + stringPost);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this , android.R.layout.simple_list_item_1,
arrayList);
setListAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
private class GetNameData extends AsyncTask< Object , Void , JSONObject> {
@Override
protected JSONObject doInBackground(Object... objects) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
URL bloFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=20");
HttpURLConnection connection = (HttpURLConnection) bloFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
StringBuilder builder = new StringBuilder();
byte[] array = new byte[8092];
int read = 0;
while ((read = inputStream.read(array)) != -1) {
builder.append(new String(array, 0, read));
}
jsonResponse = new JSONObject(builder.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
mBlogData = jsonObject;
updateListView();
}
}
}
JSON
{
status: "ok",
count: 20,
count_total: 1949,
pages: 98,
posts: [
{
id: 24680,
url: "http://blog.teamtreehouse.com/create-sticky-navigation",
title: "How to Create a Sticky Navigation",
date: "2015-02-05 14:44:13",
author: "Guil Hernandez",
thumbnail: "http://blog.teamtreehouse.com/wp-content/uploads/2015/01/sticky-150x150.jpg"
},
ECT ......
答案 0 :(得分:0)
您可以创建另一个列表并保存所需的网址,就像使用arrayList
一样。然后在列表项上单击执行以下操作:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String url = urlsList.get(position); // your new urls list
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
答案 1 :(得分:0)
列表视图中的顺序与您正在使用的数组中的顺序不同,因为您正在过滤数据,因此您可以使用帖子保留网址,如果您使用对象会更好。
答案 2 :(得分:0)
这是我提出的解决方案。这是非常邋。的。如果你有更好的想法/或建议,请告诉我
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
JSONArray jsonArrayy = null;
ArrayList<String> urlList = new ArrayList<String>();
try {
jsonArrayy = mBlogData.getJSONArray("posts");
for ( int i = 0 ; i < jsonArrayy.length() ; i++) {
JSONObject post = jsonArrayy.getJSONObject(i);
String stringPost = post.getString(KEY_AUTHOR);
String url = post.getString("url");
Log.v(TAG , stringPost);
if (stringPost.equals(mName)) {
urlList.add(url);
} else {
Log.v(TAG , "no match " + stringPost);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
String urlTwo = urlList.get(position);
// try {
// jsonArrayy = mBlogData.getJSONArray("posts");
// JSONObject jPost = jsonArrayy.getJSONObject(position);
// String url = jPost.getString("url");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlTwo));
startActivity(intent);
}