Answer the question
In order to leave comments, you need to log in
How to redraw View in Fragment?
Hello! Help me figure it out, if I understand correctly, then the ViewPager's setOffscreenPageLimit property allows you to tell it how much View to store in memory. I set the value to 0, so I was hoping to get a new redraw of the View each time when switching between fragments. And now, either nothing is redrawn, or I'm not adding to the ListView correctly. I have an overridden OnCreateView method in which, depending on the page number, some kind of View is drawn. So I tried all day to make it so that by clicking on an element of one ListView, transfer it to another fragment and display it in another ListView. Created an adapter and specified data update:
mAda = new ArrayAdapterItem(getActivity(),basketCollection);
mAda.setNotifyOnChange(true);
//пока что пытаюсь добавить хоть какой то элемент, но тут должен быть именно тот, который пользователь
//выбрал в ListView
mAda.add(new PorductItem("fdsaf", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
else if(pageNumber == 1)
{
view = inflater.inflate(R.layout.fragment_basket,null);
mAda.add(new PorductItem("1", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
basketList = (ListView) view.findViewById(R.id.list_view_basket);
basketList.setAdapter(mAda);
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
mAda = new ArrayAdapterItem(getActivity(),basketCollection);
mAda.setNotifyOnChange(true);
if (pageNumber == 0) {
view = inflater.inflate(R.layout.fragment, null);
ArrayList<Products> prodCollectionForSearch = new ArrayList<>();
//prodCollectionForSearch = Query(SelectCityScreen.mCity);
ResultSet rs1;
try {
Statement statement = MainActivity.connect.createStatement();
rs1 = statement.executeQuery("select top 10 *\n" +
"from EXPORT_TABLE\n" +
"where City_name = 'Москва' AND Quantity = 1");
while (rs1.next())
{
prodCollectionForSearch.add(new Products(rs1.getString("Product_name")));
}
ArrayAdapterProduct prodAdapter = new ArrayAdapterProduct(getActivity().getApplicationContext(),prodCollectionForSearch);
mAutoCompleteSearchProduct = (AutoCompleteTextView) view.findViewById(R.id.search_product_autoCompl);
mAutoCompleteSearchProduct.setAdapter(prodAdapter);
} catch (SQLException e) {
e.printStackTrace();
}
ArrayList<PorductItem> productCollection = new ArrayList<>();
ResultSet rs2;
try {
Statement statement = MainActivity.connect.createStatement();
rs2 = statement.executeQuery("select TOP 50 * \n" +
"from EXPORT_TABLE\n" +
"where City_name = '"+ SelectCityScreen.mCity +"' AND Quantity = 1");
while (rs2.next())
{
productCollection.add(new PorductItem(rs2.getString("Product_name"),
rs2.getString("Price"),
rs2.getString("shop_name"),
rs2.getString("Barcode_value"),
rs2.getString("address")));
}
final ArrayAdapterItem mAdap = new ArrayAdapterItem(getActivity(),productCollection);
mList = (ListView) view.findViewById(R.id.liiiiiiiiiiiiiiiist);
mList.setAdapter(mAdap);
mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// basketCollection.add((PorductItem) parent.getItemAtPosition(position));
// basketCollection.add(new PorductItem("fdsaf", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
//
mAda.add(new PorductItem("fdsaf", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
// mAda.notifyDataSetChanged();
// mAda = new ArrayAdapterItem(getActivity(),basketCollection);
// basketList = (ListView) view.findViewById(R.id.list_view_basket);
// mAda.notifyDataSetChanged();
// basketList.setAdapter(mAda);
Toast.makeText(getActivity(),"Товар" + position + "был добавлен в корзину", Toast.LENGTH_LONG).show();
return false;
}
});
} catch (SQLException e) {
e.printStackTrace();
}
return view;
}
else if(pageNumber == 1)
{
view = inflater.inflate(R.layout.fragment_basket,null);
// mAda.add(new PorductItem("2", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
// mAda.add(new PorductItem("3", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
// mAda = new ArrayAdapterItem(getActivity(),basketCollection);
mAda.add(new PorductItem("1", "fdaf", "fdasf", "fdasfasd", "fasdfas"));
basketList = (ListView) view.findViewById(R.id.list_view_basket);
basketList.setAdapter(mAda);
return view;
}
else
{
view = inflater.inflate(R.layout.fragment_sale,null);
return view;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question