How do I Set TextFields from ParseQueryAdapter? - android-fragments

I have a ParseQueryAdapter that is populating a listview inside of a fragment:
QueryAdapter:
public class CustomAdapter extends ParseQueryAdapter implements AdapterView.OnItemClickListener {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Invoice");
query.whereEqualTo("CustomerID", ParseUser.getCurrentUser());
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View cellview, ViewGroup parent) {
if (cellview == null) {
cellview = View.inflate(getContext(), R.layout.cell_layout, null);
}
TextView cellDate = (TextView) cellview.findViewById(R.id.dateTextView);
TextView invoice = (TextView) cellview.findViewById(R.id.invoiceNoTextView);
cellDate.setText(object.getString("Date"));
invoice.setText(object.getString("InvoiceNumber"));
return cellview;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
Fragment Containing Listview:
public class InvoiceHistory extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_invoice_history, container, false);
ListView invoicesListView = (ListView) layout.findViewById(R.id.invoiceList);
ParseQueryAdapter adapter = new CustomAdapter(getActivity());
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment detailView = new InvoicesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeholder, detailView);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
invoicesListView.setAdapter(adapter);
adapter.loadObjects();
return layout;
}
}
This populates the ListView with the proper fields from the query, but when I click on one of the cells the fragment transaction takes place, but i can't figure out how to set textfields on that new fragment with ParseObject Keys from the Query.
This is the fragment that gets Presented that I want to set the Textfields in:
public class InvoicesFragment extends Fragment {
public InvoicesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
return layout;
}
Android is super new to me so maybe this is a wrong way to approach it. But all i want to do here is have that detail fragment show up with populated TextViews. Im not really sure what I'm doing.

You could approach this by setting up a constructor in your DetailsFragment that you can pass in the textview info you want to show. In the example below, the data and invoice number are passed into the constructor. When the fragment is set up, this fragment is set up with the Parse object's data:
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ParseObject object = adapter.getItem(i);
Fragment detailView = DetailsFragment.newInstance(object.getString("Date"),
object.getString("InvoiceNumber"));
// ...
}
});
Here's a portion of the DetailsFragment showing the new constructor as well as how you could use them in setting up your view:
// InvoicesFragment.java
public static class InvoicesFragment extends Fragment {
// ...
public static InvoicesFragment newInstance(String invoiceDate, String invoiceNumber) {
InvoicesFragment f = new InvoicesFragment();
Bundle args = new Bundle();
args.putString("invoiceDate", InvoiceDate);
args.putString("invoiceNumber", InvoiceNumber);
f.setArguments(args);
return f;
}
public String getInvoiceDate() {
return getArguments().getString("invoiceDate", "");
}
public String getInvoiceNumber() {
return getArguments().getString("invoiceNumber", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
// Assign some TextView, ex: dateTextView
dateTextView.setText(getInvoiceDate());
return layout;
}
}

Related

NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference

I am trying to implement viewpager with Tablayout. In this everything is fine working properly, but the one problem is when i come back from another screen view pager lost their position with Tablayout and when i scroll its crash the app.I find many solutions but all of these cannot work for this. Here is my code Adapter Class:-
public class Portfolio_Adapter extends FragmentStatePagerAdapter {
int tabCount;
public Portfolio_Adapter(FragmentManager fm,int tabCount) {
super(fm);
this.tabCount=tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Websites_Fragment websites_fragment=new Websites_Fragment();
return websites_fragment;
case 1:
Mobileapplication_fragment mobileapplication_fragment=new Mobileapplication_fragment();
return mobileapplication_fragment;
case 2:
Graphics_Fragment graphics_fragment=new Graphics_Fragment();
return graphics_fragment;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
Fragment class:-
public class Portfolio_fragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.portfolio_fragment, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Portfolio");
tabLayout = (TabLayout) view.findViewById(R.id.portfolio_tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) view.findViewById(R.id.portfolio_pager);
Portfolio_Adapter adapter = new Portfolio_Adapter(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
View tab = slidingTabStrip.getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tab.getLayoutParams();
layoutParams.weight = 1;
tab.setLayoutParams(layoutParams);
}
return view;
}
}
Your code is right you only have to change the placement of code put your
tabLayout.setupWithViewPager(viewPager);
,
tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
after setadapter in viewPager
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);

I want to add Listview in second page of ViewPager In android

I have ViewPagerwitch shows some Images on first page,but on the second page i want to show some images in listview, Confusing about how to make separate page setting in layout.
Here is ViewPager method per for adding pages
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
here is adapter
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
Screen slider ViewPager class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
// Set the title view to show the page number.
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.title_template_step, mPageNumber + 1));
return rootView;
}
You can change getItem at ScreenSlidePagerAdapter. At ScreenSlideSecondPageFragment you inflate layout for second page.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ScreenSlidePageFragment.create(position);
case 1:
return ScreenSlideSecondPageFragment.create(position);
default:
return null;
}
}

Can't change the fragment's TextView on realtime

So I got 2 fragments and an activity. Those 2 fragments are communicating each other via an interface.
Problem is, I can't change the TextView in the second fragment from Activity wih some text from fragment1.
There is the same problem( How to change fragment's textView's text from activity ) but there is no real solution so i thought i should open this.
Fragment 1:
public class test_layout extends Fragment {
DearListener activityCommander;
TextView custom_question_bar, list_number;
String selectedCustomQuestion;
boolean buttonClicked;
Button yiyecekButton;
Context c;
public interface DearListener {
public void getMyText(String theQuestion);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommander = (DearListener) activity;
Log.i("MYLOG", "onAttach");
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_test_layout, container, false);
//custom_question_bar= (TextView) v.findViewById(R.id.custom_question_bar);
//list_number= (TextView) v.findViewById(R.id.list_number);
yiyecekButton = (Button) v.findViewById(R.id.yiyecekButton);
Log.i("MYLOG", "Button referenced");
yiyecekButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Log.i("MYLOG", "onCLickListener Called");
onClickYiyecek(v);
}
}
);
return v;
}
public void onClickYiyecek(View v){
Log.i("MYLOG", "OnClickYiyecek");
activityCommander.getMyText("En sevdiğim yiyecek ne?");
}
public void onClickQuestionReady(View v){
if( custom_question_bar == null)
{
Toast.makeText(c, "Boşluğu doldurmadınız", Toast.LENGTH_SHORT).show();
}
activityCommander.getMyText(custom_question_bar.getText().toString());
}
}
And the Activity:
public class NewTest extends Activity implements test_layout.DearListener {
FragmentManager manager;
test_layout test_layout_frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
manager=getFragmentManager();
test_layout_frag = new test_layout();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.the_frame_layout, test_layout_frag);
transaction.commit();
}
#Override
public void getMyText(String theQuestion) {
manager=getFragmentManager();
answer_layout answer_layout_frag = new answer_layout();
answer_layout answer_layout_obj = (answer_layout)getFragmentManager().findFragmentById(R.id.fragment2);
answer_layout_obj.changeText(theQuestion);
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.the_frame_layout, answer_layout_frag);
transaction.commit();
Log.i("MYLOG", "Transaction Called");
//transaction.addToBackStack(null);
}
}
And the second fragment:
public class answer_layout extends Fragment {
TextView yourSelectedQuestion;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_answer_layout, container, false);
yourSelectedQuestion= (TextView) v.findViewById(R.id.yourSelectedQuestion);
return v;
}
public void changeText(String a) {
Log.i("MYLOG", "changeText");
Log.i("MYLOG", a);
yourSelectedQuestion.setText(a);
}
}
No exception is given but the TextView in second fragment isn't changing.
Since i didn't write the xml here, I can if its needed. Using FrameLayout and it contains 2 fragments inside it.
In myFragment class write a static method:
public static myFragment newInstance(String txt) {
myFragment myfragment = new myFragment();
Bundle args = new Bundle();
args.putString("TEXT", txt);
myfragment.setArguments(args);
return myfragment;
}
after that in OnCreate method of myFragment class inflate the textView tv and
tv.setText(getArguments().getString("TEXT"));
In main FragmentActivity class do:
myFragment fragment = myFragment.newInstance(textToBeSent);
before replace,
fragmentTransaction.replace(R.id.fr_dynamic, fragment);
fragmentTransaction.commit();

Saving GridView state in a Fragment (Android) on Back Button

In my android application, I am switching between the two set of images-(Set1, Set2) displayed in a GridView-A inside a Fragment-A.I am doing this on a button press using a boolean variable. Clicking on any GridView icon leads to another Fragment-B. The problem is when I press back button on Fragment-B , the Fragment-A is always loaded with the images of Set1 by default. I want the same set of images (Set1 or Set2 ) to be loaded on FramentA that were displayed before going to FragmentB.
EDITED:
Fragment Code
public class GridViewFragment extends Fragment {
Context context;
GridView GridMenu;
GridViewAdapter ga;
Button Btn_Settings;
Button Btn_lang_Ch;
Button favoriteDuas;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//fragment = new GridViewFragment();
if (view == null) {
context = inflater.getContext();
view = inflater.inflate(R.layout.fragment_gridview, container, false);
ga = new GridViewAdapter(context);
Btn_lang_Ch = (Button) view.findViewById(R.id.lng_ch);
Btn_Settings = (Button) view.findViewById(R.id.button_settings);
favoriteDuas = (Button) view.findViewById(R.id.btn_favorite_duas);
GridMenu = (GridView) view.findViewById(R.id.gridView1);
GridMenu.setAdapter(ga);
} else {
// remove view from previously attached ViewGroup
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Btn_lang_Ch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ga.changeImages(!ga.imageSetChange);
ga.Lang_Status();
}
});
GridMenu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
((MainActivity) context).loadSingleDuaFragment();
}
});
super.onActivityCreated(savedInstanceState);
}
}
GridView Adapter
public class GridViewAdapter extends BaseAdapter {
public boolean imageSetChange = false;
public static boolean curr_lang = false;//english
public Integer[] mThumbIds = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
public Integer[] mThumbIds1 = {
R.drawable.urdu_dua1, R.drawable.urdu_dua2,
R.drawable.urdu_dua3, R.drawable.urdu_dua4,
R.drawable.urdu_dua5, R.drawable.urdu_dua6,
R.drawable.urdu_dua7, R.drawable.urdu_dua8,
R.drawable.urdu_dua9, R.drawable.urdu_dua10,
R.drawable.urdu_dua11, R.drawable.urdu_dua12,
R.drawable.urdu_dua13, R.drawable.urdu_dua14,
R.drawable.urdu_dua15, R.drawable.urdu_dua16,
R.drawable.urdu_dua17, R.drawable.urdu_dua18,
R.drawable.urdu_dua19, R.drawable.urdu_dua20,
R.drawable.urdu_dua21
};
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View MyView;
if (convertView == null) {
LayoutInflater li = ((Activity) mContext).getLayoutInflater();
MyView = li.inflate(R.layout.menuitem, parent, false);
} else {
MyView = (View) convertView;
}
ImageView iv = (ImageView) MyView.findViewById(R.id.image);
if (imageSetChange) {
iv.setImageResource(mThumbIds1[position]);
} else {
iv.setImageResource(mThumbIds[position]);
}
return MyView;
}
public void changeImages(boolean change) {
this.imageSetChange = change;
notifyDataSetChanged();
// gf.Lang_Status();
}
public void Lang_Status() {
// if curr_eng
if (!curr_lang)
this.curr_lang = true; // change to urdu
// if curr_urdu
else
this.curr_lang = false; // change to english
}
}
EDIT:
MainActivity
public void loadGridViewFragment() {
if (activityActive) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(getSupportFragmentManager().findFragmentByTag(GridViewFragment.TAG)!=null){
ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
}
else
{
frag = new GridViewFragment();
ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
}
ft.commit();
}
}
So, can anybody help me out regarding what should be done to load the previous set of images on coming back to gridview fragment instead of having the default images of Set1.

View inside a Fragment isn't destroyable?

i have a problem with the android Fragment class. I already implemented a FragmentStatePagerAdapter etc. Now my problem is the following: I added my own View-class to each "LSDataFragment":
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
mConnection = ((WorkActivity) this.getActivity()).getConnection(args
.getInt(ARG_OBJECT));
rootView = inflater.inflate(R.layout.view_pager, container, false);
mView = new LSDataView(this.getActivity().getApplicationContext(),(WorkActivity) this.getActivity());
update();
return rootView;
}
public void update(){
final RelativeLayout mainLayout = (RelativeLayout) rootView.findViewById(R.id.pagerMainLayout);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mainLayout.addView(mView, params);
mainLayout.invalidate();
//doesn't work outside the uiThread?!
((WorkActivity) this.getActivity()).runOnUiThread(new Runnable() {
#Override
public void run() {
mainLayout.setBackgroundColor(Color.BLUE);
mainLayout.addView(mView, params);
mainLayout.invalidate();
}
});
}
But when i remove the Fragment from my FragmentStatePagerAdapter, the LSDataView mView is remaining on the screen, and not destroyed (wtf):
for(Connection c : WorkActivity.this.mConnections){
if(c.getHandler() == handler)
WorkActivity.this.mConnections.remove(c);
}
WorkActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
mCollectionPagerAdapter.notifyDataSetChanged();
}
});

Resources