使用JsonObject创建listview但是不能传递onPostExcute()

最后发布: 2018-09-26 14:38:58


问题

您好,我试图在服务器上使用json文件创建listView。 我已经将json文件解析为一个对象类,并将其放置在aysnc扩展类的arraylist中,但是在onPostExcute函数上,我无法将数组传递给menu_layout类以将其表示为listview,因为我在该类中的arraylist是空值。

这是我的代码:

public class MainActivity extends AppCompatActivity {

Button click;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    click = (Button) findViewById(R.id.button);

    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fetchData process = new fetchData();
            process.execute();
        }
    });

}
}

这是我的代码:

public class fetchData extends AsyncTask <Void, Void, Void>{

String data = "";

String name;
String price;
String imgUrl;

public static ArrayList<food_item> menu;

@Override
protected Void doInBackground(Void... voids) {

    try
    {
        URL url = new URL("https://api.myjson.com/bins/pfevw");

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = "";
        while(line != null){
            line = bufferedReader.readLine();
            data = data + line;
        }

        JSONArray JA = new JSONArray(data);
        for(int i = 0; i < JA.length(); i++) {
            JSONObject JO = (JSONObject) JA.get(i);

            name = JO.getString("name");
            price = JO.getString("price");
            imgUrl = JO.getString("img");

            food_item item = new food_item(name, price, imgUrl);

            menu.add(item);
        }

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

    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    menu_layout.food.addAll(menu);
}
}

这是我的代码:

public class menu_layout extends AppCompatActivity {

private static final String TAG = "menu_layout";

public static ArrayList<food_item> food = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_layout);

    ListView mListView = (ListView) findViewById(R.id.menu);
    Log.d(TAG, "onCreate: Started");

    FoodAdapter adapter = new FoodAdapter(menu_layout.this, R.layout.adapter_view_layout, food);
    mListView.setAdapter(adapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "onItemClick: Name: " + food.get(position));

            //Toast.makeText(MainActivity.this, "You Clicked on: " + food.get(position).getName(), Toast.LENGTH_SHORT).show();
        }
    });
}
}

如您所见,我在主类中创建了一个按钮来执行fetchdata类以获取json文件并将其创建为对象,而不是将其传递给menu_layout类以将其显示为listView。 但是它不会将对象数组从fetchdata类传递给menu_layout类。

android json listview arraylist