使用json rereiver php mysql填充片段中的listview

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1)

我是相当新的片段,但我的代码对我来说似乎合乎逻辑但是它仍然崩溃或者根本没有结果我不知道它出了什么问题,我从php api中删除了一个json文件,包括“annances”广告解析它们并将它们列在片段内的listview中,这里是我的片段类和适配器

如果有人可以参考我可以学习的代码,或者给出一个线索我在哪里出错了,谢谢

public class MainFragment extends Fragment{

private String URLstring = "http://192.168.1.38/api/annonce/read.php";
private static ProgressDialog mProgressDialog;
private ListView lv;
ArrayList<DataModel> dataModelArrayList = new ArrayList<DataModel> ();
private ListAdapter1 listAdapter1;

public static MainFragment newInstance() {
    MainFragment fragment = new MainFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    View v=inflater.inflate(R.layout.main_fragment, container, false);


    lv  = v.findViewById(R.id.lv);

    new JsonTask().execute(URLstring); 
    return v;
}

private class JsonTask extends AsyncTask<String, String, String> {
    protected void onPreExecute() {

    }

    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line;
            String file_name="";
            while ((line = reader.readLine()) != null) {
                try {
                    JSONObject dataobj = new JSONObject(line);
                    DataModel playerModel = new DataModel();
                    playerModel.setPhoto_principale(dataobj.getString("Photo_principale"));
                    playerModel.setTitre_annonce(dataobj.getString("Titre_annonce"));
                    playerModel.setDescription(dataobj.getString("Description"));
                    playerModel.setQuartier(dataobj.getString("Quartier"));
                    dataModelArrayList.add(playerModel);
                }
                catch (Exception e){}
                buffer.append(line + "\n");
            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        setupListview();

    }



}

private void setupListview(){
    removeSimpleProgressDialog();  //will remove progress dialog
    listAdapter1 = new ListAdapter1(getActivity(),dataModelArrayList); // Here we talk about getActivity() because you are using Fragment not 
    //an actual activity so you call it getActivity()
    lv.setAdapter(listAdapter1);

}

public static void removeSimpleProgressDialog() {
    try {
        if (mProgressDialog != null) {
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }
    } catch (IllegalArgumentException ie) {
        ie.printStackTrace();

    } catch (RuntimeException re) {
        re.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

这是Adapter类

public class ListAdapter1 extends BaseAdapter {

private Context context;
private ArrayList<DataModel> dataModelArrayList;
public ListAdapter1(Context context, ArrayList<DataModel> dataModelArrayList) {

    this.context = context;
    this.dataModelArrayList = dataModelArrayList;
}
@Override
public int getViewTypeCount() {
    return getCount();
}
@Override
public int getItemViewType(int position) {

    return position;
}

@Override
public int getCount() {
    return dataModelArrayList.size();
}

@Override
public Object getItem(int position) {
    return dataModelArrayList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_list, null);

        holder.photo_principale = (ImageView) convertView.findViewById(R.id.image);
        holder.titre_annonce = (TextView) convertView.findViewById(R.id.titre_annonce);
        holder.description = (TextView) convertView.findViewById(R.id.description);
        holder.quartier = (TextView) convertView.findViewById(R.id.quartier);

        convertView.setTag(holder);
    }else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder)convertView.getTag();
    }

    Picasso.get().load(dataModelArrayList.get(position).getPhoto_principale()).into(holder.photo_principale);
    holder.titre_annonce.setText(dataModelArrayList.get(position).getTitre_annonce());
    holder.description.setText(dataModelArrayList.get(position).getDescription());
    holder.quartier.setText(dataModelArrayList.get(position).getQuartier());

    return convertView;
}

private class ViewHolder {
    // changed protected to public
    public  TextView titre_annonce, description, quartier;
    public ImageView photo_principale;
}
提问于
用户回答回答于

使用volley库来处理JSON。 看看这个答案 你如何使用Android Volley API?

其他链接

https://www.geeksforgeeks.org/volley-library-in-android/ https://developer.android.com/training/volley/index.html

一些重要的检查站

  1. 测试计算机上的URL
  2. 测试您的手机是否连接到本地主机,请访问此链接https://www.webucator.com/blog/2016/05/accessing-your-local-web-server-from-a-mobile-device-using-xampp/

与此同时

在onCreateView方法中实例化适配器类并传递相关参数

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

View v=inflater.inflate(R.layout.main_fragment, container, false);


lv  = v.findViewById(R.id.lv);
//note this change
listAdapter1 = new ListAdapter1(getActivity(),dataModelArrayList);

new JsonTask().execute(URLstring); 
return v;

}

但为了简化你的工作,下次再使用凌空

扫码关注云+社区

领取腾讯云代金券