gpt4 book ai didi

java - GridView不显示任何元素

转载 作者:行者123 更新时间:2023-12-02 02:18:01 24 4
gpt4 key购买 nike

我的 GridView 应该显示传递给 CustomAdapter 的元素,但根本没有任何条目。该应用程序从数据库中获取数据,一切正常。它获取数组中的所有元素。发送请求以接收数据的代码位于 fragment 中。这是获取数据的代码:

private class RetrieveData extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... voids) {
String stringURL = "xyz_php_file";
URL url = null;
StringBuffer stringBuffer = new StringBuffer();

try {
url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";

while((line = bufferedReader.readLine()) != null){
stringBuffer.append(line);
}

bufferedReader.close();
inputStream.close();

} catch (IOException e) {
e.printStackTrace();
}

return stringBuffer.toString();
}

@Override
protected void onPostExecute(String result) {

String username, name, description;
ArrayList<HashMap<String, String>> location = null;
int i;

try {
JSONArray data = new JSONArray(result);
location = new ArrayList<>();
HashMap<String, String> map;

for(i = 0; i < data.length(); i++){
JSONObject object = data.getJSONObject(i);

map = new HashMap<>();
map.put("username", object.getString("username"));
map.put("name", object.getString("name"));
map.put("description", object.getString("description"));
location.add(map);
}

} catch (JSONException e) {
e.printStackTrace();
}

if(location != null){
for(i = 0; i < location.size(); i++){
username = location.get(i).get("username").toString();
name = location.get(i).get("name").toString();
description = location.get(i).get("description").toString();

products.add(new Product(username, name, description));
}
}
}
}

这是 fragment 中的一些代码:

private GridView mainGridView;
private ArrayList<Product> products = new ArrayList<>();

现在这就是我在 fragment 中初始化 GridView 和 CustomAdapter 的方式:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);

new RetrieveData().execute();

TextView data = (TextView) view.findViewById(R.id.data);
if(products != null){
data.setText("Data loaded successfully");
}else{
data.setText("Error while downloading");
}

mainGridView = (GridView) view.findViewById(R.id.mealsgridview);
CustomAdapter arrayAdapter = new CustomAdapter(view.getContext(), products);
mainGridView.setAdapter(arrayAdapter);
return view;
}

我的 CustomAdapter 类:

public class CustomAdapter extends BaseAdapter {

private Context context;
private List<Product> productsList;

public CustomAdapter(Context context, ArrayList<Product> sentProduct){
this.context = context;
this.productsList = sentProduct;
}

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

@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) {

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

convertView = layoutInflater.inflate(R.layout.main_grid_layout, parent, false);

Product product = productsList.get(position);

TextView name = (TextView) convertView.findViewById(R.id.name);

TextView price = (TextView) convertView.findViewById(R.id.description);

name.setText(product.getName());

description.setText(product.getDescription());

return convertView;
}

}

最后是我的产品类别:

public class Product {

private String username;
private String name;
private String description;

public Product(String username, String name, String description) {
this.username = username;
this.name = name;
this.description = description;
}

public String getUsername() {
return username;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

}

当我启动应用程序并切换到主 fragment 时,它显示“数据加载成功”,所以我相信应用程序从数据库获取所有数据,并且 ArrayList 不是空的,但 GridView 中没有显示任何元素。哪里可能有问题?我将感谢您的回答!我已经阅读了一些教程,观看了视频,但我的代码仍然无法运行。

最佳答案

关闭 for 循环后立即调用此代码行

    mainGridView = (GridView) view.findViewById(R.id.mealsgridview);
CustomAdapter arrayAdapter = new CustomAdapter(view.getContext(), products);
mainGridView.setAdapter(arrayAdapter);

我的意思是在这个 for 循环之后

for(i = 0; i < location.size(); i++){
username = location.get(i).get("username").toString();
name = location.get(i).get("name").toString();
description = location.get(i).get("description").toString();

products.add(new Product(username, name, description));
}

关于java - GridView不显示任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48987100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com