Android 空白活动在导航菜单上打开单击?
解决:编辑:我用这个不需要自定义适配器类的简单代码解决了它。我在一节课上就解决了这个问题。希望对一些人有益Android 空白活动在导航菜单上打开单击?,android,json,navigation-drawer,Android,Json,Navigation Drawer,解决:编辑:我用这个不需要自定义适配器类的简单代码解决了它。我在一节课上就解决了这个问题。希望对一些人有益 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
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();
}
}
公共类新闻获取扩展了SherlockFragment{
字符串myJSON;
私有静态最终字符串标记_RESULTS=“result”;
字符串id;
私有静态最终字符串标记\u NAME=“title”;
私有静态最终字符串标记_ADD=Html.fromHtml(“内容”).toString();
JSONArray-peoples=null;
ArrayList个人主义者;
列表视图列表;
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气器。充气(R.layout.news\u fetch,container,false);
liste=(ListView)rootView.findViewById(R.id.ListView);
personList=新的ArrayList();
getData();
//id=getActivity().getIntent().getExtras().getString(“id”);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
//单条新闻获取歌曲信息的选择
Toast.makeText(getActivity().getApplicationContext(),“我被点击了”,Toast.LENGTH\u SHORT.show();
}
});
返回rootView;
}
受保护的无效显示列表(){
试一试{
JSONObject jsonObj=新的JSONObject(myJSON);
peoples=jsonObj.getJSONArray(标记结果);
对于(int i=0;i我想知道的第一件事是您是否使用相同的类代码和不同的url?第二件事是您如何更改菜单选项选择上的片段?知道我使用不同的类和不同的url。我正在通过fragmentTransaction ft=getSupportFragmentMana更改片段ger().beginTransaction();//定位开关(位置){case 0:ft.replace(R.id.content_-frame,fragment1);break;case 1:ft.replace(R.id.content_-frame,fragment2);break;case 2:ft.replace(R.id.content_frame,fragment3);break;}ft.commit();我想知道的第一件事是您是否使用相同的类代码和不同的url?第二件事是您如何更改菜单选项选择上的片段?知道我使用的是具有不同url的不同类。我正在通过fragmentTransaction ft=getSupportFragmentManager()更改片段.beginTransaction();//定位位置开关(位置){case 0:ft.replace(R.id.content_frame,fragment1);break;case 1:ft.replace(R.id.content_frame,fragment2);break;case 2:ft.replace(R.id.content_frame,fragment3);break;}ft.commit();
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);
}
}
}