我正在尝试使用json从数据库加载数据并将其显示在片段的listview中。我有两个类,一个叫做下载器,另一个叫做perser。我想要的是将这些数据添加到fragmnet中的listview。
我怎样才能实现这一点,因为它在content_mani中运行良好,但我想让它分段
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;
}
}
答案 0 :(得分:0)
我假设您可以从服务器正确检索JSON文件。以下代码是我为我的应用程序所做的。
class LoadDataAsyn extends AsyncTask<Void,Void,Void>{
JSONArray data;
@Override
protected void onPreExecute() {
super.onPreExecute();
// you can use ProgressDialog to indicate request start;
}
@Override
protected Void doInBackground(Void... params) {
//User is a custom class and contactlist return a Jsonarray
data = new User().ContactList();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (data!=null){
if (adapter==null){
adapter = new CustomAdapter(context.this,data);
listView.setAdapter(adapter);
}else{
adapter.Refresh(data);
}
}
}
}
我的代码中的想法是直接将JSON数组传递给适配器 在我的自定义类中:
public class CustomerAdapter extends BaseAdapter {
Context context;
JSONArray data;
public SearchSchoolAdapter (Context context,JSONArray data){
this.context= context;
this.data = data;
}
@Override
public int getCount() {
if (data!=null){
return data.length();
}
return 0;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
OneHolder holder;
if (convertView == null||!(convertView.getTag() instanceof OneHolder)){
convertView = LayoutInflater.from(context).inflate(R.layout.listrow_layout,null);
holder = new OneHolder();
holder.image = (ImageView)convertView.findViewById(R.id.pic);
holder.full =(TextView)convertView.findViewById(R.id.fullname);
convertView.setTag(holder);
}else{
holder = (OneHolder)convertView.getTag();
}
if (data!=null&&data.length()>position){
try {
JSONObject one = data.getJSONObject(position);
holder.full.setText(one.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
return convertView;
}
class OneHolder {
ImageView image;
TextView full,abb;
}
public void Refersh(JSONArray data){
this.data=data;
notifyDataSetChanged();
}
} 我不知道这是不是你要问的,希望它可以帮助你。