Java can';从活动更改为片段后,不会得到相同的结果
我开始使用导航抽屉,为此我开始使用片段。。在我更改了此活动并将代码稍作更改移动到新片段后。。我在片段中的OnOptions ItemSelected方法上得到一个错误。。我应该把网址放在哪里,如果它不是正确的地方放 这是FoodView.java活动Java can';从活动更改为片段后,不会得到相同的结果,java,android,android-fragments,android-fragmentactivity,Java,Android,Android Fragments,Android Fragmentactivity,我开始使用导航抽屉,为此我开始使用片段。。在我更改了此活动并将代码稍作更改移动到新片段后。。我在片段中的OnOptions ItemSelected方法上得到一个错误。。我应该把网址放在哪里,如果它不是正确的地方放 这是FoodView.java活动 public class FoodView extends AppCompatActivity { private ListView lvFood; @Override protected void onCreate(Bundle savedI
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);
}
}
将这些方法保留在活动中,从片段中删除它们
@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) {
// add this line in your activity
((FoodViewFragment)selectedFragment).yourServerCallMethodInFragment();
return true;
}
return super.onOptionsItemSelected(item);
}
在活动级别维护当前片段
private Fragment selectedFragment;
并在中获取所选片段的引用
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
setTitle("Food View");
selectedFragment = new FoodViewFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame,selectedFragment, "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;
}
现在,您的片段将具有此方法
public void yourServerCallMethodInFragment(){
new JSONTask().execute("http://10.0.3.2/MySqlDemo/food.php");
}
如果(id==R.id.action\u refresh){//在活动selectedFragment.yourServerCallMethodInFragment();return true;}中添加这一行,希望对您有所帮助。上面的部分显示了一个错误。。。selectedFragment.yourServerCallMethodInFragment();你可以让我知道这是什么类型的错误。我想应该是类型转换问题,试试这个,否则让我知道((FoodViewFragment)selectedFragment);
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
setTitle("Food View");
selectedFragment = new FoodViewFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame,selectedFragment, "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;
}
public void yourServerCallMethodInFragment(){
new JSONTask().execute("http://10.0.3.2/MySqlDemo/food.php");
}