【问题标题】:My intents for my sorted listview won't work properly我的排序列表视图的意图将无法正常工作
【发布时间】:2015-02-09 22:08:17
【问题描述】:

我有一个 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();
    }
}

但是因为数据被过滤掉了......它打开了错误的网址!!!我不知道如何设置我的意图,因此它将获取过滤数据的 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"
},

等等……

【问题讨论】:

    标签: android arrays json listview android-intent


    【解决方案1】:

    您可以创建另一个列表并保存所需的 url,就像您使用 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);
    }
    

    【讨论】:

      【解决方案2】:

      listview中的顺序和你使用的数组不一样,因为你是过滤数据,所以可以把url和post一起保留,如果用object会更好。

      【讨论】:

        【解决方案3】:

        这是我想出的解决方案。这是非常草率的。如果您有更好的想法/或建议,请告诉我

        @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);
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-24
          • 1970-01-01
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-08
          相关资源
          最近更新 更多