Replacing Fragment in ViewPager shows blank screen? - android

I'm having a lot of trouble getting Fragments to work with Tabs/Swiping. I have one activity and two Fragments, one which has a ListView. What I want is when I swipe from fragment 1 to the fragment with the ListView, to refresh the ListView or if possible recreate the second fragment....
What I am trying right now is to use FragmentTransaction.replace() like this:
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
Fragment frag = new FragmentTwo();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
if (arg0.getPosition() == 1) {
trans.replace(R.id.pager, frag);
trans.addToBackStack(null);
trans.commit();
}
}
So I thought that when I swipe to tab 2, which is the one with the ListView, it would replace it with a new one, which it creates. What is actually happening is that when I swipe to tab 2, it recreates the fragment with the ListView (I know because I have a dialog in the onCreateView which shows to the screen) but then the fragment/tab is blank/black. What am I doing wrong? Where does this fragment which is created go?
I don't know if the code of the fragments is relevant but please let me know if I should post it and I can do that. This is my PagerAdapter class.
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
Thanks for any help! I'm really pulling my hair out over this one :(
Edit:
When you swipe from tab 1 to tab 2, tab 2 shows my dialog and then loads the tab, but if I go back to tab 1 its all black...and if I then go to tab 2 again, it creates again and shows my dialog and then goes black as well...
Edit: (adding listview fragment code)
public class FragmentTwo extends Fragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser_Helpers2 jParser = new JSONParser_Helpers2();
ArrayList<HashMap<String, String>> tripList;
// url to get all products list
private static String url_all_trips = "http://10.0.2.2/android_connect/get_all_trips.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TRIPS = "trips";
private static final String TAG_TRIPID = "tripid";
private static final String TAG_TRIPNAME = "tripName";
private static final String TAG_UID = "uid32";
// products JSONArray
JSONArray trips = null;
View rootView;
Button btnRefresh;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View rootView = inflater.inflate(R.layout.fragment_one, container, false);
rootView = inflater.inflate(R.layout.fragment_two, container, false);
btnRefresh = (Button) rootView.findViewById(R.id.refresh);
// Hashmap for ListView
tripList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = (ListView) rootView.findViewById(R.id.list);
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//getting values from selected ListItem
String tripid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
//starting new intent
Intent in = new Intent(getActivity(), EditTrip_Activity.class);
//sending pid to next activity
in.putExtra(TAG_TRIPID, tripid);
//starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
return rootView;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
ListView lv = (ListView) rootView.findViewById(R.id.list);
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading your trips. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_trips, "GET", params);
// Check your log cat for JSON response
Log.d("All Trips: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
trips = json.getJSONArray(TAG_TRIPS);
// looping through All Products
for (int i = 0; i < trips.length(); i++) {
JSONObject c = trips.getJSONObject(i);
// Storing each json item in variable
String tripid = c.getString(TAG_TRIPID);
String tripname = c.getString(TAG_TRIPNAME);
String userId = c.getString("uid");
// creating new HashMap
DatabaseHandler_Helpers db = new DatabaseHandler_Helpers(getActivity());
HashMap<String, String> map = new HashMap<String, String>();
if (userId.equals(db.getUserDetails().get("uid"))) {
// adding each child node to HashMap key => value
map.put(TAG_TRIPID, tripid);
map.put(TAG_TRIPNAME, tripname);
// adding HashList to ArrayList
tripList.add(map);
} //else {
//map.put(TAG_TRIPID, "");
//map.put(TAG_TRIPNAME, "You have no tracked trips.");
//tripList.add(map);
//}
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(),
NewTrip_Activity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
((Activity) getActivity()).runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), tripList,
R.layout.list_item, new String[] { TAG_TRIPID,
TAG_TRIPNAME},
new int[] { R.id.pid, R.id.name });
// updating listview
((ListView) lv.findViewById(R.id.list)).setAdapter(adapter);
}
});
}
}
}

Try to extend FragmentStatePagerAdapter instead of FragmentPagerAdapter.
Here's my code for fragments and it works fine for me:
//Adapter
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//For example, this is one fragment:
public class Database extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View databaseview = inflater.inflate(R.layout.database, container, false);
return databaseview;
}
}
//My MainActivity
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
Let me know if you also want the implementation of the listview in any one fragment.
Hope this helps..:)

My children fragments are in a ViewPager. I just replace the ViewPage adapter by FragmentStatePagerAdapter instead of FragmentPagerAdapter
Child Fragment
....
mViewPager.setAdapter(new SearchOriginTabsPagerAdapter(getFragmentManager()));
Adapter...
public class SearchOriginTabsPagerAdapter extends FragmentStatePagerAdapter {
public SearchOriginTabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new NearbyFragment();
case 1:
return new RecentOriginFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}

Alright.. Here's the thing..
I didn't really use your code but used similar concept. Its gonna be a complete solution.
//This is the starting point of the app. It has many fragments and I am using my own json to get the data.
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
//This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method.
public class Android extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
// private ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListView lv1;
private static final String QUESTION = "question";
private static final String ANSWERS = "answer";
// ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mikeview = inflater.inflate(R.layout.androidlayout, container, false);
return mikeview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ExpandableListView expListView = null;
try{
expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList);
}
catch (Exception e){
e.printStackTrace();
}
new Thread(){
#Override
public void run(){
}
}.start();
try {
setParent();
prepareChild();
} catch (Exception e) {
e.printStackTrace();
}
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/*Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();*/
return true;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/* Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/*Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();*/
}
});
}
public void setParent(){
listDataHeader = new ArrayList<String>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int my =0;my<array.length();my++){
JSONObject c = array.getJSONObject(my);
String topics = c.getString(QUESTION);
listDataHeader.add(topics);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void prepareChild() throws JSONException {
listDataChild = new HashMap<String, List<String>>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int mz = 0;mz<array.length();mz++){
ArrayList<String> child = new ArrayList<String>();
JSONObject d = array.getJSONObject(mz);
String ans = d.getString(ANSWERS);
child = new ArrayList<String>();
child.add(ans);
int position = mz ;
listDataChild.put(listDataHeader.get( position),child);
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("android.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
//This is the adapter.
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.newlistitems,null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.newlistviewitems);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater myInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = myInflater.inflate(R.layout.newlistgroup, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.newlistviewgroup);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

If anyone using viewpager2 then use the below constructor.
FragmentStateAdapter(fragmentManager, lifecycle)

Related

How to replace the baseadapter value and how to stop the pagination loading in android filter screen?

I have worked with the concept of filter that have to filter the job from job list based on skills and some list or there.
https://postimg.org/image/g3p1z6lbd/ - DashBoard Fragment.
About DashBoardFragment:
Contains job list view.
Dash Filter Button. - which redirect to the Filter screen.
public class DashBoardRefactor extends Fragment {
public static ProgressDialog progress;
public static List<DashListModel> dashRowList1 = new ArrayList<DashListModel>();
public static View footerView;
// #Bind(R.id.dashListView)
public static ListView dashListView;
int preCount = 2, scroll_Inc = 10, lastCount;
boolean flag = true;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dashboard_fragment, container, false);
ButterKnife.bind(this, v);
setHasOptionsMenu(true);
progress = new ProgressDialog(getActivity());
dashListView = (ListView) v.findViewById(R.id.dashListView);
footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dashboard_list_footer, null, false);
dashListView.addFooterView(footerView);
footerView.setVisibility(View.GONE);
dashRowList1.clear();
dashboardViewTask();
dashListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("onItemClick", "onItemClick <---- ");
Intent toPositionDetail = new Intent(getActivity(), PositionDetailScreenRefactor.class);
toPositionDetail.putExtra("id", dashRowList1.get(position).getDashId());
startActivity(toPositionDetail);
getActivity().overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
}
});
final int totalJobCount = SessionStores.gettotalJobList(getActivity());
Log.e("totalJobCount", "totalJobCount----" + totalJobCount);
dashListView.setOnScrollListener(new EndlessScrollListener(getActivity(), dashListView, footerView));
return v;
}
public void dashboardViewTask() {
progress.setMessage("Please Wait. It is Loading..job orders....");
progress.setCanceledOnTouchOutside(false);
progress.setCancelable(false);
progress.show();
// footerView.setVisibility(View.VISIBLE);
Map<String, String> params = new HashMap<String, String>();
Log.e("candidate_id", "candidate_id---->" + SessionStores.getBullHornId(getActivity()));
params.put("candidate_id", SessionStores.getBullHornId(getActivity()));
params.put("page", "1");
new DashBoardTask(getActivity(), params, dashListView, footerView);
// progress.dismiss();
}
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
if (menu != null) {
menu.removeItem(R.id.menu_notify);
}
inflater.inflate(R.menu.menu_options, menu);
MenuItem item = menu.findItem(R.id.menu_filter);
item.setVisible(true);
getActivity().invalidateOptionsMenu();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.his__menu_accept:
Toast.makeText(getActivity(), "clicked dashboard menu accept", Toast.LENGTH_LONG).show();
return true;
case R.id.menu_filter:
// click evnt for filter
Toast.makeText(getActivity(), "clicked dashboard filter", Toast.LENGTH_LONG).show();
Intent filter_intent = new Intent(getActivity(), DashBoardFilterScreen.class);
startActivity(filter_intent);
getActivity().overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onPause() {
super.onPause();
// dashboardViewTask();
}}
DashBoardTask:
public class DashBoardTask {
public DashBoardTask(Context context, Map<String, String> params, ListView dashListView, View footerView) {
this.context = context;
Log.e("context ", "DashBoardTask: " + context);
this.dashListView = dashListView;
this.params = params;
this.footerView = footerView;
ResponseTask();
}
private void ResponseTask() {
new ServerResponse(ApiClass.getApiUrl(Constants.DASHBOARD_VIEW)).getJSONObjectfromURL(ServerResponse.RequestType.POST, params, authorizationKey, context, "", new VolleyResponseListener() {
#Override
public void onError(String message) {
if (DashBoardRefactor.progress.isShowing()) {
DashBoardRefactor.progress.dismiss();
}
}
#Override
public void onResponse(String response) {
//Getting Response and Assign into model Class
int currentPosition = dashListView.getFirstVisiblePosition();
dashListAdapter = new DashListAdapter(context, DashBoardRefactor.dashRowList1, dashListView);
dashListView.setAdapter(dashListAdapter);
((BaseAdapter) dashListAdapter).notifyDataSetChanged();
if (currentPosition != 0) {
// Setting new scroll position
dashListView.setSelectionFromTop(currentPosition + 1, 0);
}
if (footerView.isShown()) {
footerView.setVisibility(View.GONE);
}
//progress.dismiss();
if (DashBoardRefactor.progress.isShowing()) {
try {
DashBoardRefactor.progress.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
DashListAdapter:
________________
public class DashListAdapter extends BaseAdapter {
public static ListView dashListView;
Context c;
private LayoutInflater inflater;
private List<DashListModel> dashRowList;
public DashListAdapter(Context c, List<DashListModel> dashRowList, ListView dashListView) {
this.c = c;
this.dashListView = dashListView;
this.dashRowList = dashRowList;
}
#Override
public int getCount() {
return this.dashRowList.size();
}
#Override
public Object getItem(int position) {
return dashRowList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder dashHolder;
if (inflater == null)
inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.dashboard_jobdetails_list, null);
Log.e("get pos", "get pooooossss---->" + dashRowList.get(position));
final DashListModel dashModel = dashRowList.get(position);
dashHolder = new ViewHolder(convertView);
//Assign the value into screen
dashHolder.dash_company_name.setText(dashModel.getDashCompanyName());
}
the above code for displaying dashboard fragment list.
https://postimg.org/image/nqvp1dud9/ - This link is FilterScreen
By using this image if i filter the job based on the designed UI detail. That should replace into the DashboadFragment list The result should display into the DashBoard Fragment. How can I add pagination on Filter screen the same which have in DashBoardFragment.

How to send ID from an Element on listItems to another View where i wana publish all information of this Element?

This ListView class where i bring different element from my server with help of php and JSON calls to my listview. The problem is i would like to send the ID of element to another view when i click of specific element on listview. and in another View i would like to publish the detail information about the element that i clicked. Any wan have idea about that?
public class Forestillinger extends AppCompatActivity {
ListView dagensaktivitet;
TextView txt;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
JSONParser jsonParser = new JSONParser();
JSONObject hc = null;
JSONArray fstilling;
public String hk = "";
int success;
String msg;
final Context c = this;
StableArrayAdapter adb;
private static final String url_Forestillinger = "http://barnestasjonen.no/test/db_get_forestillinger.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forestillinger);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dagensaktivitet = (ListView) findViewById(R.id.forestillinglist);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView txt = (TextView) view.findViewById(android.R.id.text1);
txt.setTextColor(Color.WHITE);
return view;
}
};
dagensaktivitet.setAdapter(adapter);
Log.d("i oncreate", adapter.toString());
//if(!fromLocalDB())
try {
new getShows().execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//JSONObject tickets = task.getJSon();
adapter.notifyDataSetChanged();
dagensaktivitet.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), Forestilling.class);
i.putExtra("selectedItem", listItems.get(position));
startActivity(i);
}
});
}
public void fromExtDB() throws JSONException {
fstilling = hc.getJSONArray("forestillinger");
for (int i = 0; i < fstilling.length(); i++) {
listItems.add(fstilling.getJSONObject(i).getString("tittel")+fstilling.getJSONObject(i).getString("id"));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_forestillinger, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void forestillingResult(){
new AlertDialog.Builder(Forestillinger.this)
.setTitle("OBS!!!")
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
hk = "";
finish();
}
})
.show();
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
List<Integer> trialId;
public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects, List<Integer> objectId) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); i++)
mIdMap.put(objects.get(i), i);
trialId = objectId;
}
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
public int getActualId(int position)
{
return trialId.get(position);
}
}
class getShows extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... args) {
Log.d("TEST", "jeg er her");
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("id", "adam#gmail.com"));
//params.add(new BasicNameValuePair("type", "android"));
JSONObject json = jsonParser.makeHttpRequest(url_Forestillinger, "POST", params);
try {
if (json != null) {
success = Integer.parseInt(json.getString("success"));
msg = json.toString();
System.out.println("Vi er her:" + msg + " " + success);
}
if (success == 1) {
hc = json;
System.out.println("jojo"+json.toString());
}
else
{
msg=json.getString("message");
System.out.println(msg);
msg= Html.fromHtml(msg).toString();
hk = msg;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
//run code
try {
Log.d("TAG23", "vi er in onpostExecute");
if(success == 1) {
fromExtDB();
}else{
forestillingResult();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
and here is the second view.
public class Forestilling extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forestilling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*Bundle extras = getIntent().getExtras();
Log.d("TAG:", "" + extras.getInt("TryThis"));*/
Bundle extras = getIntent().getExtras();
String s = extras.getString("selectedItem");
Log.d("TAG:", "" + s);
TextView tit = (TextView)findViewById(R.id.tittel);
TextView dato = (TextView)findViewById(R.id.dato);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_forestilling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'm not allowed to flag at this moment, but this might be a duplicate of how can i use the number from textfield of each row in listview and make a call on this number?
Anyway, my answer given there might solve your problem:
Here is what I do:
private class MyAdapter extends BaseAdapter {
private ListView listView;
...
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
this.listView = (ListView) viewGroup;
and then in each View.OnClickListener I can get the position of the clicked item by
#Override
public void onClick(View view) {
...
MyAdapter.this.listView.getPositionForView((View) view.getParent()));

How to pass a row in listview in a fragment to listview in another fragment in same activity when click button in row?

I have two fragments in one activity and there is a ViewPager element in main_activity.xml. I have created two different classes that exteds Fragment class for each fragments and overrided onCreateView methods. The first fragment is a channels list. So it's a listview that its each row consist of a textview and a button. I want when the user click the button, get passed the row to listView in second fragment. I created a interface in ChannelsFragment named FavButtonClickedListener and overrided this interface in MainActivity.java. Then I added onAttach() method into ChannelsFragment in order to reference MainActivity. In getView method of Adapter of ChannelsFragment class I have set the clicklistener to imageButtons in each row and I called whenFavButtonClicked.onFavButtonClicked(); My code is working for SharedPreferences hovewer I need to restart the application to see changes in favorites list. I want to add the rows dynamically. I don't know where I wrong please help me
ChannelsFragment :
public class ChannelsFragment extends Fragment {
ViewGroup rootVg;
Context context;
FavButtonClickedListener whenFavButtonClicked;
private final List<String> favValues = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.channels_list_layout, container, false);
final ListView channelsList = (ListView) rootView.findViewById(R.id.channelsListView);
String[] chNames = new String[]{"ChA","ChB","ChC"};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < chNames.length; ++i) {
list.add(chNames[i]);
}
//set adapter to listview
MyListAdapter listAdapter = new MyListAdapter(getActivity(),chNames);
channelsList.setAdapter(listAdapter);
return rootView;
}
public interface FavButtonClickedListener {
public void onFavButtonClicked(int position,List<String>favs);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
whenFavButtonClicked = (FavButtonClickedListener)context;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
}
ChannelsFragment Adapter class:
public class MyListAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
URL channelUrl;
public MyListAdapter(Context context, String[] values) {
super(context, R.layout.list_item_complex, values);
this.context = context;
this.values = values;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public long getCount(int position) {
// TODO Auto-generated method stub
return values.length;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_complex, parent, false);
TextView chNameText = (TextView) rowView.findViewById(R.id.chName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.chImage);
final ImageButton imageButton = (ImageButton)rowView.findViewById(R.id.starButon);
Button chButton = (Button)rowView.findViewById(R.id.chButon);
chNameText.setText(values[position]);
//Set the channels' logo appropriately
if(values[position].equals("CHa")){
imageView.setImageResource(R.drawable.CHaImage);
imageButton.setId(0);
}else if(values[position].equals("CHb")){
imageView.setImageResource(R.drawable.CHbImage);
imageButton.setId(1);
}else if(values[position].equals("Chc")){
imageView.setImageResource(R.drawable.CHcImage);
imageButton.setId(2);
}else{
imageView.setImageResource(R.drawable.defult_bacground_logo);
}
}
//Determine the target url for each channel correctly
chButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("android.intent.action.Activity2");
String chName = null;
if(values[position].equals("CHa")){
i.putExtra("CHa", "http://www.cha.com/");
chName = "CHa";
i.putExtra("nameofCh", ChName);
startActivity(i);
}
if(values[position].equals("CHb")){
i.putExtra("CHb", "http://www.chb.com/");
chName = "CHb";
i.putExtra("nameofCh",ChName);
startActivity(i);
}
if(values[position].equals("CHc")){
i.putExtra("CHc", "http://www.chc.com/");
chName = "CHc";
i.putExtra("nameofCh",ChName);
startActivity(i);
}
});
final Context konteks;
konteks = this.getContext();
//set button click for favorite channels list
// I use this imageButton for create a favorites list in listview in FavoritesChannels
//fragment. I want when the user click this button, the row that has been clicked, get
//passed to FavoritesChannels fragment
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.button_states_star);
SharedPreferences prefs = konteks.getSharedPreferences("favorites",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if(imageButton.getId()==0){
editor.putBoolean("CHaChecked", true);
editor.commit();
favValues.add("CHa");
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHa have been added to favlist", Toast.LENGTH_SHORT).show();
}
else if(imageButton.getId()==1){
editor.putBoolean("CHbChecked", true);
favValues.add("CHb");
editor.commit();
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHb have been added to favlist", Toast.LENGTH_SHORT).show();
}
else if(imageButton.getId()==2){
editor.putBoolean("CHcChecked", true);
favValues.add("CHc");
editor.commit();
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHc have been added to favlist", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), "CH have been added to favlist", Toast.LENGTH_SHORT).show();
}
}
});
return rowView;
}
}
MainActivity.java:
onFavButtonClicked() implementation is at end of file
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener,KanallarFragment.FavButtonClickedListener {
private static final int NUM_PAGES = 2;
public SharedPreferences prefs;
FragmentTransaction fragmentTransaction;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
//Initiate Actionbar instance and define its tab names
private ActionBar actionBar;
private String[] tabs = { "Channels", "Favorites"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragMgr = getSupportFragmentManager();
fragmentTransaction = fragMgr.beginTransaction();
fragmentTransaction.add(new FavoriteFragment(), "favsFragment");
fragmentTransaction.add(new ChannelsFragment(), "channelsFragment");
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//ViewPager's PageChangeListener
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
//Retrieve actionbar and set some properties
actionBar = getSupportActionBar();
//actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setStackedBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_gradient));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
// Adding Tabs to actionbar
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
for(int i = 0; i<actionBar.getTabCount(); i++){
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_layout, null);
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(tabs[i]);
ImageView tabImage = (ImageView)customView.findViewById(R.id.tab_icon);
if(i==0){
tabImage.setImageResource(R.drawable.television_icon_64);
}else{
tabImage.setImageResource(R.drawable.star_icon);
}
actionBar.getTabAt(i).setCustomView(customView);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}*/
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new ChannelsFragment();
case 1:
return new FavoritesFragment();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
#Override
public void onTabReselected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
mPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public void onFavButtonClicked(int position,List<String>favs) {
// TODO Auto-generated method stub
FavoritesFragment favFrag = (FavoritesFragment)getSupportFragmentManager().findFragmentByTag("favsFragment");
if(favFrag !=null){
favFrag.updateList(position,favs);
}
}
And FavoritesFragment class that contain updateList() method :
public class FavorilerFragment extends Fragment {
Context context;
private final List<String> values = new ArrayList<String>();
private final List<String> favValues = new ArrayList<String>();
ListView listView;
TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.favorites_list_layout, container, false);
listView = (ListView)rootView.findViewById(R.id.f_channels_ListView);
textView = (TextView)rootView.findViewById(R.id.favListMessage);
SharedPreferences prefs = context.getSharedPreferences("favorites",getActivity().MODE_PRIVATE);
//-----SharedPreferences values for adapter----
if(prefs.getBoolean("CHaChecked", false)){
values.add("Cha");
}
if(prefs.getBoolean("CHbChecked", false)){
values.add("Chb");
}
if(prefs.getBoolean("ChcChecked", false)){
values.add("Chc");
}
//-----------------------------------------------
if(values.size() == 0){
textView.setText("Favorites List is empty");
}else{
textView.setText("");
}
MyListAdapter adapter = new MyListAdapter(getActivity(),values);
listView.setAdapter(adapter);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
}
public void updateList(int position,List<String> argfavValues){
for(int a= 0;a<argFavValues.size();a++){
favValues.add(argFavValues.get(a));
}
MyListAdapter = new MyListAdapter(getActivity(),favValues);
adaptor.notifyDataSetChanged();
listView.setAdapter(adapter);
}
public static class MyListAdapter extends ArrayAdapter<String>{
private final Context context;
private final List<String> values = new ArrayList<String>();
URL ChUrl;
public MyListAdapter(Context context, List<String> values) {
super(context, R.layout.list_item_complex_fav, values);
this.context = context;
for(int i=0;i<values.size();i++){
this.values.add(values.get(i));
}
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public long getCount(int position) {
// TODO Auto-generated method stub
return values.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_complex_fav, parent, false);
TextView chNameText = (TextView) rowView.findViewById(R.id.ChName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ChannelsImage);
Button chButton = (Button)rowView.findViewById(R.id.chButon);
chNameText.setText(values.get(position));
//Set the channels' logo appropriately
if(values.get(position).equals("CHa")){
imageView.setImageResource(R.drawable.CHaImage);
}else if(values.get(position).equals("CHb")){
imageView.setImageResource(R.drawable.CHbImage);
}else if(values.get(position).equals("CHc")){
imageView.setImageResource(R.drawable.CHcImage);
else{
imageView.setImageResource(R.drawable.default_logo);
}
return rowView;
}
}
Per the official documentation, fragment to fragment communication should be routed through the activity.
You need to create an interface in your channels fragment with some callback method defined, like onSelected. When a list item is selected, call that interface method using the activity context:
public class ChannelsFragment extends ListFragment {
OnItemSelectedListener mCallback;
public interface OnItemSelectedListener {
public void onSelected(int position);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnItemSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onSelected(position);
}
}
Then, your activity needs to implement ChannelFragment.OnItemSelectedListener and define the callback method.
public void onSelected(int position) {
Fragment2 fragment = (Fragment2)
getSupportFragmentManager().findFragmentById(R.id.fragment2);
if (fragment != null) {
fragment.updateValue(position);
}
}
You can pass data via parameters. To send the data to the other fragment, create a public method in the fragment which can be called directly from the activity.

Viewpager, Cursor and Fragment

I am new to the viewpager and was wondering if anyone could point me to a tutorial or source code of a project that uses a viewpager with fragments and a database. I've seen examples of PagerAdapters but I'm just not getting how they all work together (Cursor, Fragment and PagerAdapter)
Thanks in advance.
I have posted some answers in the other posts related to your question.
Here's some of the links that you might find helpful.
First Link: To get custom views in every page slide. (Accepted answer)
Android:How to create different view in ViewPager?
Second: How to properly use fragments with ViewPager. (Accepted answer)
How to properly use fragments with ViewPager?
If those two links are not that helpful, try this:
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
//This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method.
public class Android extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
// private ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListView lv1;
private static final String QUESTION = "question";
private static final String ANSWERS = "answer";
// ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mikeview = inflater.inflate(R.layout.androidlayout, container, false);
return mikeview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ExpandableListView expListView = null;
try{
expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList);
}
catch (Exception e){
e.printStackTrace();
}
new Thread(){
#Override
public void run(){
}
}.start();
try {
setParent();
prepareChild();
} catch (Exception e) {
e.printStackTrace();
}
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/*Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();*/
return true;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/* Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/*Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();*/
}
});
}
public void setParent(){
listDataHeader = new ArrayList<String>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int my =0;my<array.length();my++){
JSONObject c = array.getJSONObject(my);
String topics = c.getString(QUESTION);
listDataHeader.add(topics);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void prepareChild() throws JSONException {
listDataChild = new HashMap<String, List<String>>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int mz = 0;mz<array.length();mz++){
ArrayList<String> child = new ArrayList<String>();
JSONObject d = array.getJSONObject(mz);
String ans = d.getString(ANSWERS);
child = new ArrayList<String>();
child.add(ans);
int position = mz ;
listDataChild.put(listDataHeader.get( position),child);
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("android.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
//This is the adapter.
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.newlistitems,null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.newlistviewitems);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater myInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = myInflater.inflate(R.layout.newlistgroup, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.newlistviewgroup);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Lemme know if you need some more examples on that..:)
You need to create abstract class adapter to use both cursor and pager adapter and extend to your own adapter and bind the view.
public abstract class CursorPagerAdapter extends PagerAdapter {
public static final String TAG = CursorPagerAdapter.class.getSimpleName();
protected WeakReference<Context> ctxRef;
protected boolean mDataValid;
protected boolean mAutoRequery;
protected Cursor mCursor;
protected int mRowIDColumn;
protected ChangeObserver mChangeObserver;
protected DataSetObserver mDataSetObserver;
/**
* If set the adapter will call requery() on the cursor whenever a content change notification is
* delivered. Implies {#link #FLAG_REGISTER_CONTENT_OBSERVER}.
*
* #deprecated This option is discouraged, as it results in Cursor queries being performed on the
* application's UI thread and thus can cause poor responsiveness or even Application
* Not Responding errors. As an alternative, use {#link android.app.LoaderManager} with
* a {#link android.content.CursorLoader}.
*/
#Deprecated
public static final int FLAG_AUTO_REQUERY = 0x01;
/**
* If set the adapter will register a content observer on the cursor and will call {#link
* # onContentChanged()} when a notification comes in. Be careful when using this flag: you will
* need to unset the current Cursor from the adapter to avoid leaks due to its registered
* observers. This flag is not needed when using a CursorAdapter with a {#link
* android.content.CursorLoader}.
*/
public static final int FLAG_REGISTER_CONTENT_OBSERVER = 0x02;
public CursorPagerAdapter(Context context, Cursor c, int flags) {
init(context, c, flags);
}
void init(Context context, Cursor c, int flags) {
if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {
flags |= FLAG_REGISTER_CONTENT_OBSERVER;
mAutoRequery = true;
} else {
mAutoRequery = false;
}
boolean cursorPresent = c != null;
mCursor = c;
mDataValid = cursorPresent;
ctxRef = new WeakReference<>(context);
mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
mChangeObserver = new ChangeObserver();
mDataSetObserver = new MyDataSetObserver();
} else {
mChangeObserver = null;
mDataSetObserver = null;
}
if (cursorPresent) {
if (mChangeObserver != null) {
c.registerContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
c.registerDataSetObserver(mDataSetObserver);
}
}
}
public Cursor getCursor() {
return mCursor;
}
#Override
public int getCount() {
if (mDataValid && mCursor != null)
return mCursor.getCount();
else
return 0;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
Context context = ctxRef.get();
if (context != null) {
View v = newView(context, mCursor, (ViewGroup) container);
bindView(v, context, mCursor);
((ViewPager) container).addView(v);
return v;
} else {
throw new IllegalStateException("context is null");
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
public abstract View newView(Context context, Cursor cursor, ViewGroup parent);
public abstract void bindView(View view, Context context, Cursor cursor);
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) return null;
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if (mChangeObserver != null) {
oldCursor.unregisterContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
}
mCursor = newCursor;
if (newCursor != null) {
if (mChangeObserver != null) {
newCursor.registerContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
newCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
notifyDataSetChanged();
}
return oldCursor;
}
private void onContentChange() {
if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
mDataValid = mCursor.requery();
}
}
private class ChangeObserver extends ContentObserver {
public ChangeObserver() {
super(new Handler());
}
/**
*
* #return True if self-change notifications should be delivered to the observer.
*/
#Override
public boolean deliverSelfNotifications() {
return true;
}
/**
* This method is called when a content change occurs.
* #param selfChange
*/
#Override
public void onChange(boolean selfChange) {
onContentChange();
}
}
private class MyDataSetObserver extends DataSetObserver {
#Override
public void onChanged() {
mDataValid = true;
notifyDataSetChanged();
}
#Override
public void onInvalidated() {
mDataValid = false;
notifyDataSetChanged();
}
}
}
Sample Adapter (in kotlin):
class SampleAdapter(context: Context, cursor: Cursor?, autoQuery: Int): CursorPagerAdapter(context, cursor, autoQuery) {
internal var mContext: Context
internal var mLayoutInflater: LayoutInflater
internal var aQuery: AQuery
val imageWidth = Helper.getDisplayWidth(context)
init {
mContext = context
mLayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
aQuery = AQuery(context)
}
override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View {
return mLayoutInflater.inflate(R.layout.your_item_row, parent, false)
}
override fun bindView(view: View?, context: Context?, cursor: Cursor?) {
val sampleVo = SampleVo.getValueFromCursor(cursor)
val tvTitle = view?.findViewById<AppCompatTextView>(R.id.tvTitle)
tvTitle?.text = sampleVo.title
}
Original Source: link

Using viewpager in my application

I want to use viewpager in my application.I'm tried to do this everyday in one month but i can't achieve the solution.I want to create pages with same listview concept but different datas.Here is my code:
public final class TestFragment extends ListFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
ArrayList <HashMap<String, Object>> imageliste = new ArrayList<HashMap<String, Object>>();
public class MyCustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
//Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String,Object>> imageliste) {
super(context, textViewResourceId,imageliste);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=LayoutInflater.from(getActivity());
row=inflater.inflate(R.layout.list, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.text1);
label.setText((CharSequence) imageliste.get(position).get("Baslik"));
TextView label2=(TextView)row.findViewById(R.id.text2);
int boyut =imageliste.get(position).get("Desc").toString().length();
label2.setText((CharSequence) imageliste.get(position).get("Desc").toString().substring(0, (boyut/3)*2)+"...");
ImageView icon=(ImageView)row.findViewById(R.id.img);
icon.setImageDrawable((Drawable) imageliste.get(position).get("Resim"));
return row;
}
}
public String getURLContent(String url)
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
} catch (ClientProtocolException e) {
return "";
} catch (IOException e) {
return "";
}
}
public ArrayList<HashMap<String, Object>> getImageLinks(String strng){
ArrayList<HashMap<String, Object>> myBooks2 = new ArrayList<HashMap<String, Object>>();
String html = getURLContent(strng);
Document doc = Jsoup.parse(html);
Elements divs = doc.getElementsByClass("postBox");
for (Element div : divs) {
Element masthead = div.select("img[src].attachment-post-thumbnail").first();
String linkHref = masthead.attr("src");
Element masthead2 = div.select("h1").first().select("a").first();
String baslik = masthead2.text();
Element masthead3 = div.select("div.textPreview").first().select("p").first();
String desc = masthead3.text();
//Drawable drawable = LoadImageFromWebOperations();
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("Resim", LoadImageFromWebOperations(linkHref));
hm.put("Baslik", baslik);
hm.put("Desc", desc);
myBooks2.add(hm);
}
return myBooks2;
}
private Drawable LoadImageFromWebOperations(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
public class backgroundLoadListView extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(getActivity());
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
//adapter.notifyDataSetChanged();
dialog.dismiss();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.setMessage("Yükleniyor...");
dialog.show();
}
#Override
protected Void doInBackground(String... arg) {
// TODO Auto-generated method stub
imageliste=getImageLinks(arg[0]);
return null;
}
}
public class backgroundLoadListView2 extends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.customslidingtabhost, null);
ListView listView1=(ListView)view.findViewById(R.id.list);
//MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.list, imageliste);
//listView1.setAdapter(adapter);
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
listView1.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView1.setDividerHeight(2);
listView1.setBackgroundColor(Color.WHITE);
((PullToRefreshListView) listView1).onRefreshComplete();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
imageliste=getImageLinks("http://www.teknoinfo.net/kategoriler/haberler/teknoloji-haberleri");
return null;
}
}
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
return fragment;
}
private String mContent = "???";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
//new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
MyCustomAdapter adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
View view = inflater.inflate(R.layout.customslidingtabhost, null);
final ListView v=(ListView)view.findViewById(R.id.list);
/*((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
new backgroundLoadListView2().execute();
}
});*/
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
v.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
v.setDividerHeight(2);
v.setBackgroundColor(Color.WHITE);
v.setAdapter(adapter);
((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
//new backgroundLoadListView2().execute();
// new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
It is hard to work through all that code you posted (most of which has not to do with your question), but as far as I can tell you have not even set up a ViewPager from your code or it is hidden in some XML file that you didn't post.
What you need to do is create a ViewPager instance. For example in your fragment's XML layout file, like:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Now, in your Fragment you create an adapter that specifies the pages that you want (similarly to how a ListAdapter specifies items to a ListView). For that, create a class (you can do that inline in the Fragment code) that inherits from PagerAdapter. For example, something like:
private class MyPagerAdapter extends PagerAdapter implements TitleProvider {
private ListView pagerListView1;
private ListView pagerListView2;
public MyPagerAdapter() {
LayoutInflater inflater = getActivity().getLayoutInflater();
pagerListView1 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
pagerListView2 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
}
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(View container, int position) {
switch (position) {
case 0:
((ViewPager) container).addView(pagerListView1, 0);
return pagerListView1;
case 1:
((ViewPager) container).addView(pagerListView2, 0);
return pagerListView2;
}
return null;
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (View) object;
}
#Override
public void finishUpdate(View container) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View container) {
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
Note that I hard-coded 2 pages/lists in this pager. You can fill those as you would with any other ListView. The layout of the pages is just a simple ListView that is inflated from the fragment_pagerlist XML file, which looks something like:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#color/BackgroundLight" />
Finally, you bind the ViewPager's adapter somewhere in your onActivityCreated method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager pager = (ViewPager) getView().findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter());
}
Note that this does not yet give you a ViewPagerIndicator. Check Jake Wharton's excellent library for that.
If you want the code to a fully implemented and working version, with multiple lists (and other views) in a ViewPager and a ViewPagerIndicator, take a look at the open source RateBeer for Android project; specifically http://code.google.com/p/ratebeerforandroid/source/browse/RateBeerForAndroid/src/com/ratebeer/android/gui/fragments/SearchFragment.java#486 for a real-world PagerAdapter.

Categories

Resources