我有一個json數組,我使用for
循環來循環每個對象以獲取作者姓名。如果作者姓名與我輸入的作者姓名相同,則該姓名將添加到數組列表中。然後使用陣列適配器調整此數組列表。我的排序列表視圖的意圖將無法正常工作
但這裏是我的問題 - 我正在使用此方法來搜索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();
}
}
但因爲數據被過濾掉....它打開了錯誤的網址!我不知道如何設置我的意圖,所以它會得到過濾數據的網址...請幫助。如果這是令人困惑的完整代碼和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 ......