使用 json rereiver php mysql 在片段中填充列表视图
Posted
技术标签:
【中文标题】使用 json rereiver php mysql 在片段中填充列表视图【英文标题】:populate a listview in fragment with a json rereiver php mysql 【发布时间】:2019-11-02 11:40:41 【问题描述】:我对片段相当陌生,但我的代码对我来说似乎足够合乎逻辑,但它仍然崩溃或根本没有结果我不知道它出了什么问题,我从 php 获取一个 json 文件api,包括解析它们并将它们列在片段内的列表视图中的“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();
这是适配器类
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;
【问题讨论】:
【参考方案1】:使用 volley 库来处理 JSON。 看看这个答案 How do you use the android Volley API?
其他链接
https://www.geeksforgeeks.org/volley-library-in-android/ https://developer.android.com/training/volley/index.html
一些重要的检查点
-
在电脑上测试url
测试你的手机是否连接到localhost,查看这个链接https://www.webucator.com/blog/2016/05/accessing-your-local-web-server-from-a-mobile-device-using-xampp/
同时
在onCreateView方法中实例化适配器类并传递相关参数
@覆盖 public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑 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;
但是为了简化你的工作,下次就用凌空抽球
【讨论】:
以上是关于使用 json rereiver php mysql 在片段中填充列表视图的主要内容,如果未能解决你的问题,请参考以下文章