在导航菜单上打开空白活动点击?

时间:2015-07-21 04:00:51

标签: android json navigation-drawer

解决:编辑:我用这个不需要自定义Adapter类的简单代码解决了它。我在一堂课中解决了这个问题。希望它对某些人有益。

public class NewsFetch extends SherlockFragment {
String myJSON;

private static final String TAG_RESULTS="result";
String id;
private static final String TAG_NAME = "title";
private static final String TAG_ADD =Html.fromHtml("content").toString();


JSONArray peoples = null;

ArrayList<HashMap<String, String>> personList;
ListView liste;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.news_fetch, container, false);
    liste = (ListView)rootView.findViewById(R.id.listViews);
    personList = new ArrayList<HashMap<String,String>>();
    getData();

    // id = getActivity().getIntent().getExtras().getString("id");

    liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // On selecting single news get song information
            Toast.makeText(getActivity().getApplicationContext(), "I was clicked", Toast.LENGTH_SHORT).show();
        }

    });
    return rootView;

}

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for (int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String titles = c.getString(TAG_NAME);
            String address = c.getString(TAG_ADD);
            HashMap<String,String> persons = new HashMap<String,String>();
            persons.put(TAG_NAME,titles);
            persons.put(TAG_ADD,address);
            personList.add(persons);

        }
        ListAdapter adapter = new SimpleAdapter(
                getActivity().getApplicationContext(), personList, R.layout.news_list,
                new String[]{TAG_NAME,TAG_ADD},
                new int[]{ R.id.titles, R.id.cont}
        );

        liste.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://10.0.2.2/json/news.php");
            Html.fromHtml(TAG_NAME).toString();
            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");
            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
            Html.fromHtml(TAG_NAME).toString();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}




        }

问题:导航抽屉里有3个菜单。当我使用相同的代码为第一个菜单选项(JSON位置和字符串是不同的)它工作正常但当我尝试使用以下代码的第二个菜单选项(具有不同的JSON位置和字符串名称)它不显示任何只是空白活动的东西都会打开并且什么都不显我在manifest.xml中使用了internet权限。任何帮助,将不胜感激。提前谢谢

public class NewNewsMain extends SherlockFragment {

private ListView listedView;
ArrayList<NewNews> newsList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.activity_newnews, container, false);
   listedView = (ListView) rootView.findViewById(R.id.listed);
   newsList= new ArrayList<>();
   new NewsAsynctask().execute("http://www.thebritishcollege.edu.np/api/news");
   return rootView;
}

public class NewsAsynctask extends AsyncTask<String,Void,Boolean> {

@Override
protected Boolean doInBackground(String... params) {
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(params[0]);
        HttpResponse response = client.execute(post);

        int status = response.getStatusLine().getStatusCode();

        if (status == 200) {

            HttpEntity entities = response.getEntity();
            String datas = EntityUtils.toString(entities);

            JSONObject jObje = new JSONObject(datas);
            JSONArray jArrays = jObje.getJSONArray("result");
            for (int i = 0; i < jArrays.length(); i++) {
                NewNews newss = new NewNews();

                JSONObject jRealsobject = jArrays.getJSONObject(i);

                newss.setTitle(jRealsobject.getString("title"));
             //   news.setThumbnail(jRealsobject.getString("thumbnail"));
                newsList.add(newss);
            }
            return true;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    if(result==true){
        NewNewsAdapter adapters= new NewNewsAdapter(getActivity().getApplicationContext(), R.layout.newnews, newsList);
        listedView.setAdapter(adapters);
    }
}

}

0 个答案:

没有答案