我的适配器项目在arraylist中被删除但listview没有更新仍然显示从数据库中删除的项目。只有在从其他片段导航到此片段时才会出现错误。
请帮助。不知道哪里出了问题。一直在打电话 以下是我的片段文件。
public class SupplierViewFragment extends Fragment {
private ArrayList<Supplier> suppliers = new ArrayList<>();
ListView lvListView;
FunDapter adapter;
ListView lvSupplierList;
TextView tvSupplierViewMessage;
private FragmentTransaction fragmentTransaction;
ViewPager pager;
private AdapterView.AdapterContextMenuInfo info;
BindDictionary<Supplier> dictionary = new BindDictionary<>();
public SupplierViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_supplier_view, container, false);
tvSupplierViewMessage = (TextView) view.findViewById(R.id.tvSupplierViewMessage);
tvSupplierViewMessage.setVisibility(View.INVISIBLE);
getData("supplier-view-servlet");
dictionary = new BindDictionary<>();
dictionary.addStringField(R.id.tvSupplierListID, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return "ID: " + item.getSupplierID();
}
});
dictionary.addStringField(R.id.tvSupplierListCompanyName, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return item.getSupplierCompanyName();
}
});
dictionary.addStringField(R.id.tvSupplierListName, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return "(" + item.getSupplierName() + ")";
}
});
dictionary.addStringField(R.id.tvSupplierListAddress, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return item.getSupplierAddress();
}
});
dictionary.addStringField(R.id.tvSupplierListTelp, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return item.getSupplierTelp();
}
});
dictionary.addStringField(R.id.tvSupplierListMobile, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return item.getSupplierMobile();
}
});
dictionary.addStringField(R.id.tvSupplierListEmail, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return item.getSupplierEmail();
}
});
dictionary.addStringField(R.id.tvSupplierListCity, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return getCityName("cities-servlet?city-id=" + item.getSupplierCity());
}
});
dictionary.addStringField(R.id.tvSupplierListCountry, new StringExtractor<Supplier>() {
@Override
public String getStringValue(Supplier item, int position) {
return getCountryName("countries-servlet?city-id=" + item.getSupplierCity());
}
});
adapter = new FunDapter(SupplierViewFragment.this.getActivity(),suppliers,R.layout.supplier_list_layout,dictionary);
lvSupplierList = (ListView) view.findViewById(R.id.lvSupplierView);
lvSupplierList.setAdapter(adapter);
adapter.notifyDataSetChanged();
registerForContextMenu(lvSupplierList);
pager = (ViewPager) getActivity().findViewById(R.id.pager);
return view;
}
public void refreshApi(){
suppliers.clear();
adapter.resetData();
getData("supplier-view-servlet");
lvSupplierList.setAdapter(adapter);
adapter.notifyDataSetChanged();
lvSupplierList.invalidate();
//adapter = new FunDapter(SupplierViewFragment.this.getActivity(),suppliers,R.layout.supplier_list_layout,dictionary);
Log.d("REFRESH", suppliers.size()+" -------------------");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String text = null;
switch (item.getItemId()){
case R.id.supplier_view_delete_id:
AlertDialog.Builder builder = new AlertDialog.Builder(SupplierViewFragment.this.getActivity());
builder.setMessage("Are sure to delete selected supplier?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Supplier supplier = new Supplier(suppliers.get(info.position).getSupplierID());
try {
Log.d("Supplier Size:",suppliers.size() + "to delete:" + info.position);
deleteData("supplier-delete-servlet?supplier-id=" + supplier.getSupplierID() );
suppliers.clear();
refreshApi();
Log.d("Supplier AFTER:",suppliers.size() + "to delete:" + info.position);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
pager.setCurrentItem(0);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(SupplierViewFragment.this.getActivity(),"Supplier Deleted",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
case R.id.supplier_view_edit_id:
//StaffEditFragment staffEditFragment = new StaffEditFragment.newInstance("Some1","Some2");
Log.d("Current Position:",""+info.position);
Supplier supplierEdit = suppliers.get(info.position);
Bundle bundle = new Bundle();
bundle.putInt("supplierID",supplierEdit.getSupplierID());
SupplierEditFragment supplierEditFragment = new SupplierEditFragment();
supplierEditFragment.setArguments(bundle);
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,supplierEditFragment);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Edit Supplier");
fragmentTransaction.commit();
default:
return super.onContextItemSelected(item);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.supplier_context_menu,menu);
}
public void deleteData(String servletAddress){
JsonRequestHelper requestHelper = new JsonRequestHelper();
JSONArray jsonArray = requestHelper.getJsonArrayViaGET(servletAddress);
}
public String getCountryName(String servletAddress){
String countryName = null;
try {
JsonRequestHelper requestHelper = new JsonRequestHelper();
JSONArray jsonArray = requestHelper.getJsonArrayViaGET(servletAddress);
for (int i = 0; i < jsonArray.length(); i++) {
countryName = jsonArray.getJSONObject(i).getString("country-name").toString();
}
}
catch (Exception e) {
e.printStackTrace();
}
Log.d("cityName:",countryName);
return countryName;
}
public String getCityName(String servletAddress){
String cityName = null;
try {
JsonRequestHelper requestHelper = new JsonRequestHelper();
JSONArray jsonArray = requestHelper.getJsonArrayViaGET(servletAddress);
for (int i = 0; i < jsonArray.length(); i++) {
cityName = jsonArray.getJSONObject(i).getString("city-name").toString();
}
}
catch (Exception e) {
e.printStackTrace();
}
Log.d("cityName:",cityName);
return cityName;
}
public void getData(String servletAddress){
JsonRequestHelper requestHelper = new JsonRequestHelper();
JSONArray jsonArray = requestHelper.getJsonArrayViaGET(servletAddress);
Supplier supplier = new Supplier();
if (jsonArray == null) {
//Log.d("Reach000","#####################");
tvSupplierViewMessage.setVisibility(View.VISIBLE);
tvSupplierViewMessage.setText("Error Connecting Database");
Toast.makeText(SupplierViewFragment.this.getActivity(), "Error Connection", Toast.LENGTH_SHORT).show();
}
else {
tvSupplierViewMessage.setVisibility(View.INVISIBLE);
tvSupplierViewMessage.setText("");
try {
for (int i = 0; i < jsonArray.length(); i++) {
supplier = new Supplier();
supplier.setSupplierID(Integer.parseInt(jsonArray.getJSONObject(i).get("id").toString()));
supplier.setSupplierCompanyName(jsonArray.getJSONObject(i).get("company-name").toString());
supplier.setSupplierName(jsonArray.getJSONObject(i).get("name").toString());
supplier.setSupplierAddress(jsonArray.getJSONObject(i).get("address").toString());
supplier.setSupplierCity(Integer.parseInt(jsonArray.getJSONObject(i).get("city-id").toString()));
supplier.setSupplierEmail(jsonArray.getJSONObject(i).get("email").toString());
supplier.setSupplierTelp(jsonArray.getJSONObject(i).get("telp").toString());
supplier.setSupplierMobile(jsonArray.getJSONObject(i).get("mobile").toString());
suppliers.add(supplier);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
您可以在适配器中添加updateDataset(ArrayList供应商)方法,并在需要更新listview内容时调用它。
在适配器中添加以下方法,
public void updateDataset(ArrayList<Supplier> suppliers) {
this.suppliers = suppliers;
notifyDatasetChanged();
}
从数据库中删除项目时,获取新数据并调用
adapter.updateDataset(ArrayList<Supplier> suppliers)
答案 1 :(得分:0)
fragmentTransaction = getFragmentManager()。beginTransaction(); fragmentTransaction.replace(R.id.main_container,new SupplierFragment()); ((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(&#34; View Supplier&#34;); fragmentTransaction.commit();
return super.onContextItemSelected(item);