I got stuck into the problem with listview used with inner fragment insideNavigation-drawer-page-sliding-tab-strip viewpager tabs, that given in This git hub example.
i am using the same example and All 4 tabs having a list view with different arraylist set to there adapters.
I am using only one fragment and bases of tabs position I am loading different list data to inner fragment arrayAdapter. Also I have two buttons one is delete and another one is Add.
What I want: If I press the add button then it should add new data to the array list(based on the tab position add the new add to respective arrayList) and refresh the listview data.
In my code its refreshing the current viewing tab but in next tab listview also refreshing and taking the data of previous tab. Please help me on this, any help would be appreciated.
I have single class file for this. If my approach is wrong please let me know correct approach.
public class PageSlidingTabStripFragment extends Fragment {
public static final String TAG = PageSlidingTabStripFragment.class
.getSimpleName();
private MyListAdapter myAdapter;
private boolean isDeleteBtnClicked = false;
private int tabType = 0;
private ListView myListView;
public static PageSlidingTabStripFragment newInstance() {
return new PageSlidingTabStripFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.pager, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view
.findViewById(R.id.tabs);
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
tabs.setViewPager(pager);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "Categories", "Home", "Top Paid",
"Top Free" };
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public SherlockFragment getItem(int position) {
return new SuperAwesomeCardFragment().newInstance(position); // here I am calling the fragment by sending the position
}
}
private ArrayList<String> categoriesList = new ArrayList<String>();
private ArrayList<String> homeList = new ArrayList<String>();
private ArrayList<String> topPaidList = new ArrayList<String>();
private ArrayList<String> topFreeList = new ArrayList<String>();
@SuppressLint("ValidFragment")
public class SuperAwesomeCardFragment extends SherlockFragment{
private final String ARG_POSITION = "position";
private int position;
private Button deleteBtn;
private Button addBtn;
public SuperAwesomeCardFragment newInstance(int position) {
SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
public SuperAwesomeCardFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoriesList.clear();
// adding element to Categories arraylist
categoriesList.add("categories -1");
categoriesList.add("categories -2");
categoriesList.add("categories -3");
categoriesList.add("categories -4");
homeList.clear();
// adding element to Home arraylist
homeList.add("home -1");
homeList.add("home -2");
homeList.add("home -3");
homeList.add("home -4");
topPaidList.clear();
// adding element to TopPaid arraylist
topPaidList.add("topPaid -1");
topPaidList.add("topPaid -2");
topPaidList.add("topPaid -3");
topPaidList.add("topPaid -4");
topFreeList.clear();
// adding element to TopFree arraylist
topFreeList.add("topFree -1");
topFreeList.add("topFree -2");
topFreeList.add("topFree -3");
topFreeList.add("topFree -4");
position = getArguments().getInt(ARG_POSITION); // here I get the position of tab/view pager
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.main_pager_body, container, false);
myListView = (ListView) root.findViewById(R.id.my_list_view);
deleteBtn = (Button) root.findViewById(R.id.delete_btn);
addBtn = (Button) root.findViewById(R.id.add_btn);
switch (position) {
// Categories position
case 0:
setListView(categoriesList);
break;
// Home position
case 1:
setListView(homeList);
break;
// TopPaid position
case 2:
setListView(topPaidList);
break;
// TopFree position
case 3:
setListView(topFreeList);
break;
} // here I am setting the list view based on the tab/view pager position.
deleteBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "deleteBtn Button clicked", Toast.LENGTH_SHORT).show();
}
});
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), " addBtn Button clicked", Toast.LENGTH_SHORT).show();
switch (position) {
// Categories position
case 0:
categoriesList.add("categories -5");
setListView(categoriesList);
break;
// Home position
case 1:
homeList.add("home -5");
setListView(homeList);
break;
// TopPaid position
case 2:
topPaidList.add("topPaid -5");
setListView(topPaidList);
break;
// TopFree position
case 3:
topFreeList.add("topFree -5");
setListView(topFreeList);
break;
}
}
});
return root;
}
public void setListView(ArrayList<String> myList){
myAdapter = new MyListAdapter(getActivity().getApplicationContext(), R.layout.list_row, myList);
myListView.setAdapter(myAdapter);
myListView.setItemsCanFocus(true);
myAdapter.notifyDataSetChanged();
}
}
public class MyListAdapter extends ArrayAdapter<String>
{
ArrayList<String> myList;
public MyListAdapter(Context context, int textViewResourceId,
ArrayList<String> myList) {
super(context, textViewResourceId, myList);
this.myList = myList;
}
public class ViewHolder {
private TextView listElementTV;
private RelativeLayout buttonContains;
private Button deleteItemBtn;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder = null;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// get the view for assign data to list view
v = vi.inflate(R.layout.list_row, null);
TextView listElementTV = (TextView) v.findViewById(R.id.list_element);
RelativeLayout buttonContains = (RelativeLayout) v.findViewById(R.id.button_contains);
Button deleteItemBtn = (Button) v.findViewById(R.id.delete_item);
holder = new ViewHolder();
holder.listElementTV = listElementTV;
holder.buttonContains = buttonContains;
holder.deleteItemBtn = deleteItemBtn;
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
// get list of hospitalityInfo using position
String listElement = this.myList.get(position);
if(listElement != null){
holder.listElementTV.setText(listElement);
}
return v;
}
}
}