0
jsonを使用してデータベースからデータをロードし、それをフラグメントビューのリストビューに表示しようとしています。私は2つのクラスを持っています.1つはダウンローダ、もう1つはperserです。私が望むのは、このデータをfragmnetのlistviewに追加することです。それはcontent_maniで正常に動作が、私はそれは私はあなたが正しくサーバからJSONファイルを取り出すことができると仮定しjsonを使用してmysqlからデータを表示し、フラグメント内のリストビュー
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import info.androidhive.materialtabs.fragments.OneFragment;
/**
* Created by GEORGE on 25/06/2016.
*/
public class Downloader extends AsyncTask<Void ,Integer,String>{
Context c;
String urladdress;
ListView lv;
ProgressDialog progressDialog;
public Downloader(MainActivity context, ListView listView, String urladdress) {
this.c = context;
this.urladdress = urladdress;
this.lv = listView;
}
// before the job start
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(c);
progressDialog.setTitle("fetch data");
progressDialog.setMessage("fetching data.....please wait");
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
String data=downloadData();
return data;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
if(s!=null){
Parser p=new Parser(c,s,lv);
p.execute();
}else {
Toast.makeText(c,"Unable to download data",Toast.LENGTH_LONG).show();
}
}
private String downloadData(){
InputStream inputStream=null;
String line=null;
try{
URL url=new URL(urladdress);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
inputStream=new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer=new StringBuffer();
if (bufferedReader!=null)
{
while ((line=bufferedReader.readLine())!=null)
{
stringBuffer.append(line+"\n");
}
}else
{
return null;
}
return stringBuffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream!=null)
{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
and
package info.androidhive.materialtabs.activity;
import android.app.*;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.*;
import info.androidhive.materialtabs.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
public class Parser extends AsyncTask<Void,Integer,Integer> {
Context c;
ListView lv;
String data;
ArrayList<String> users=new ArrayList<>();
ProgressDialog pd;
public Parser(Context c, String data, ListView lv) {
this.c = c;
this.data = data;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("parser");
pd.setMessage("parsing....please wait");
pd.show();
}
@Override
protected Integer doInBackground(Void... params) {
return this.perse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer==1){
ArrayAdapter<String> adapter=new ArrayAdapter<String>(c, android.R.layout.simple_list_item_1,users);
lv.setAdapter(adapter);
}
else {
Toast.makeText(c,"Unable to perse",Toast.LENGTH_LONG).show();
}
pd.dismiss();
}
private int perse(){
try{
JSONArray ja=new JSONArray(data);
JSONObject jo=null;
users.clear();
for (int i=0;i<ja.length();i++){
jo=ja.getJSONObject(i);
String name=jo.getString("Category_name");
String id=jo.getString("idcategories");
users.add(name);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
package info.androidhive.materialtabs.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.activity.Downloader;
public class OneFragment extends Fragment{
Button btn_categories;
String urladdress="http://192.168.0.107/android/users.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_one, container, false);
btn_categories = (Button) view.findViewById(R.id.btn_categories);
final ListView lv = (ListView) view.findViewById(R.id.lv);
//final Downloader downloader=new Downloader(this, lv, urladdress);
//downloader.execute();
return view;
}
}
PHPタグが間違っています、それはPHPコードではありません – newage