从活动更改为片段后,无法获得相同的结果

时间:2017-02-23 12:56:12

标签: java android android-fragments android-fragmentactivity

我开始使用导航抽屉,因为我开始使用片段..在我改变了这个活动并将代码稍微更改为新片段后移动了..我在片段内的onOptionsItemSelected方法上出错了..如果不是正确的地方,我应该把网址放在哪里? !!

这是FoodView.java活动

public class FoodView extends AppCompatActivity {


private ListView lvFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_food_view);


    lvFood = (ListView) findViewById(R.id.lvFood);
}

    public class JSONTask extends AsyncTask<String, String, List<FoodModel>> {


        @Override
        protected List<FoodModel> doInBackground(String... params) {

            HttpURLConnection httpURLConnection = null;
            BufferedReader reader = null;


            try {
                URL url = new URL(params[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();

                InputStream inputStream = httpURLConnection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                String finalJson = buffer.toString();


                JSONArray parentArray = new JSONArray(finalJson);

                List<FoodModel> foodModelList = new ArrayList<>();


                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    FoodModel foodModel = new FoodModel();
                    foodModel.setFood(finalObject.optString("name"));
                    foodModel.setStatus(finalObject.optString("status"));
                    foodModel.setAmount(finalObject.optInt("amount"));
                    foodModel.setDescription(finalObject.optString("description"));
                    foodModel.setDate(finalObject.optInt("exDate"));


                    foodModelList.add(foodModel);
                }

                return foodModelList;

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


            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return new ArrayList<>();
        }

        @Override
        protected void onPostExecute(List<FoodModel> result) {
            super.onPostExecute(result);

            FoodAdapter adapter = new FoodAdapter(getApplicationContext(), R.layout.row, result);
            lvFood.setAdapter(adapter);


        }}

        public class FoodAdapter extends ArrayAdapter {

            private List<FoodModel> foodModelList;
            private int resource;
            private LayoutInflater inflater;

            public FoodAdapter(Context context, int resource, List<FoodModel> objects) {
                super(context, resource, objects);
                foodModelList = objects;
                this.resource = resource;
                inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);


            }


            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = inflater.inflate(resource, null);
                }

                ImageView ivIcon;
                TextView foodName, txt_amount, txt_desc, txt_date, txt_status;

                ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                foodName = (TextView) convertView.findViewById(R.id.foodName);
                txt_amount = (TextView) convertView.findViewById(R.id.txt_amount);
                txt_desc = (TextView) convertView.findViewById(R.id.txt_desc);
               txt_status = (TextView) convertView.findViewById(R.id.txt_status);
                txt_date = (TextView) convertView.findViewById(R.id.txt_date);

                foodName.setText(foodModelList.get(position).getFood());
                txt_amount.setText("Amount: " + foodModelList.get(position).getAmount());
                txt_desc.setText(foodModelList.get(position).getDescription());
                txt_status.setText(foodModelList.get(position).getStatus());
                txt_date.setText("EX Date: " + foodModelList.get(position).getDate());


                return convertView;
            }
        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.navigation_drawer, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_refresh) {
        new JSONTask().execute("http://10.0.3.2/MySqlDemo/food.php");
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是FoodViewFragment.java Fragment

public class FoodViewFragment extends Fragment {
private ListView lvFood;
Context context = getActivity();

public FoodViewFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

     View v = inflater.inflate(R.layout.fragment_food_view, container, false);
    lvFood = (ListView)v. findViewById(R.id.lvFood);

    return v;
}

public class JSONTask extends AsyncTask<String, String, List<FoodModel>> {


    @Override
    protected List<FoodModel> doInBackground(String... params) {

        HttpURLConnection httpURLConnection = null;
        BufferedReader reader = null;


        try {
            URL url = new URL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            String finalJson = buffer.toString();


            JSONArray parentArray = new JSONArray(finalJson);

            List<FoodModel> foodModelList = new ArrayList<>();


            for (int i = 0; i < parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                FoodModel foodModel = new FoodModel();
                foodModel.setFood(finalObject.optString("name"));
                foodModel.setStatus(finalObject.optString("status"));
                foodModel.setAmount(finalObject.optInt("amount"));
                foodModel.setDescription(finalObject.optString("description"));
                foodModel.setDate(finalObject.optInt("exDate"));


                foodModelList.add(foodModel);
            }

            return foodModelList;

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


        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ArrayList<>();
    }

    @Override
    protected void onPostExecute(List<FoodModel> result) {
        super.onPostExecute(result);

        FoodViewFragment.FoodAdapter adapter = new FoodViewFragment.FoodAdapter(context, R.layout.row, result);
        lvFood.setAdapter(adapter);




    }}
public class FoodAdapter extends ArrayAdapter {

    private List<FoodModel> foodModelList;
    private int resource;
    private LayoutInflater inflater;

    public FoodAdapter(Context context, int resource, List<FoodModel> objects) {
        super(context, resource, objects);
        foodModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater)getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(resource, null);
        }

        ImageView ivIcon;
        TextView foodName, txt_amount, txt_desc, txt_date, txt_status;

        ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
        foodName = (TextView) convertView.findViewById(R.id.foodName);
        txt_amount = (TextView) convertView.findViewById(R.id.txt_amount);
        txt_desc = (TextView) convertView.findViewById(R.id.txt_desc);
        txt_status = (TextView) convertView.findViewById(R.id.txt_status);
        txt_date = (TextView) convertView.findViewById(R.id.txt_date);

        foodName.setText(foodModelList.get(position).getFood());
        txt_amount.setText("Amount: " + foodModelList.get(position).getAmount());
        txt_desc.setText(foodModelList.get(position).getDescription());
        txt_status.setText(foodModelList.get(position).getStatus());
        txt_date.setText("EX Date: " + foodModelList.get(position).getDate());


        return convertView;
    }
}

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_refresh) {
        new JSONTask().execute("http://10.0.3.2/MySqlDemo/food.php");
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是navigationDrawer.java类中代码的一部分,我试图运行nav_home项目并显示FoodViewFragmentActivity

 public boolean onNavigationItemSelected(MenuItem item) {



    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        setTitle("Food View");

       FoodViewFragment fragment = new FoodViewFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame,fragment, "fragment1");
        fragmentTransaction.commit();


    } else if (id == R.id.nav_food) {

    } else if (id == R.id.nav_money) {

    } else if (id == R.id.nav_settings) {

    } else if (id == R.id.nav_about) {

    } else if (id == R.id.nav_notifications) {

    }


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

0 个答案:

没有答案