首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用json rereiver php mysql填充片段中的listview

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

Stack Overflow用户
提问于 2019-06-20 00:57:35
回答 1查看 190关注 0票数 0

我是比较新的片段,但是我的代码对我来说似乎足够逻辑,但它仍然崩溃或根本没有结果我不知道它出了什么问题,我从php api中提取了一个json文件,其中包括解析它们的"annances“广告,并在片段内的列表视图中列出它们,这是我的片段类和适配器。

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

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;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56672634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档