Pass Json Object from ViewHolder to Fragment - java

I have a RecyclerView and a Fragment. I want to pass a JSONObject from RecyclerView to Fragment. So, I created an Interface and implemented it on Fragment and on RecyclerView. I initialized the variable and access the method in the fragment passing the JSONObject to it, however I am getting a NPE when trying to access the method:
public class ProductResultsListFragment extends Fragment implements ProductResultAdapterInterface{
//code here
#Override
public void showResultsInMap(JSONObject mapObject)
{
openMapFragment(mapObject);
}
In my RV class I have the following:
public class ProductSearchAdapter extends RecyclerView.Adapter<ProductSearchAdapter.ViewHolder> {
public ProductResultAdapterInterface mProductResultsListener;
.....
if (mapObjects.length()>0)
{
mProductResultsListener.showResultsInMap(mapObjects);
}
The if statement is inside my viewHolder in my RecyclerView class but the instance is made public.
I have tried casting my mProductResultsListener but dont know what class to cast it into.
public ProductResultAdapterInterface mProductResultsListener=((ProductResultAdapterInterface ) ?????);
Just a quick comment: the mapObject (JsonObject) is created on a onClick method inside a button in ViewHolder, I cannot pass bundle on OnCreate method.
this.btnMarkItemMap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
switch (v.getId())
{
case (R.id.markProductinMap):
String mapObj=null;
if (mapObjects !=null)
{
String positionDescription=String.valueOf(position);
String productLatLngValue=txtProductPrice.getText().toString()+";"+ txtItemLanLong.getText().toString();
//tring mapPosition=
mapObj=mapObjects.optString(String.valueOf(position));
if (mapObj!="") //Button not click remove from map
{
btnMarkItemMap.setColorFilter(Color.rgb(0, 0, 0));
btnMarkItemMap.setTag(Color.rgb(0, 0, 0));
mapObjects.remove(positionDescription);
}
else //Button Click add to Map
//
{
btnMarkItemMap.setColorFilter(Color.rgb(255, 51, 102));
btnMarkItemMap.setTag(position);
try {mapObjects.put(positionDescription, productLatLngValue);}catch (JSONException ex){}
if (mapObjects.length()>0)
{
mProductResultsListener.showResultsInMap(mapObjects);
}
}
}else{
mapObjects=new JSONObject();
}
break;
}
}
});

where have you assigned the variable mProductResultsListener?
you need to assign this to the object of the fragment by passing it into the adapter. Otherwise its value is null and you will get a NullPointeException if you call a function on a null object.
If you have, then please update the question with that code.

You can pass the data using bundles like this
SomeFragment someFragment = new SomeFragment();
Bundle bundle = new Bundle();
bundle.putString("value1", "2");
bundle.putString("value2", "23");
bundle.putString("value3", "276");
bundle.putString("value4", "27");
bundle.putBoolean("flag", true);
someFragment.setArguments(bundle);
And in SomeFragment
Bundle bundle = getArguments();
strValue1 = bundle.getString("value1");
strValue2 = bundle.getString("value2");
and so on for getting all the passed data from bundle

ProductResultsListFragment yourFragment = new ProductResultsListFragment();
Bundle bundle = new Bundle();
bundle.putString("yourJsonObject", yourJsonObject.toString);
yourFragment.setArguments(bundle);
public class ProductResultsListFragment...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle savedInfo = this.getArguments();
String savedJsonString = savedInfo.getString("yourJsonObject");
JSONObject myJsonObject = new JSONObject(savedJsonString);
//....

On my Fragment adapter init, I added:
ProductResultAdapterInterface mProInterface=(ProductResultAdapterInterface) this;
mAdapter = new ProductSearchAdapter(R.layout.product_results_cardlist,getActivity(),productListFeed,mProInterface);
on my adapter constructor I changed the signature to accept the Interface as a parameter passed from the Fragment
public ProductSearchAdapter (int rowLayout, Context context,List<Product> feedList,ProductResultAdapterInterface mProductResultsListener) {
this.feedItemLists=feedList;
this.rowLayout = rowLayout;
this.mContext = context;
this.mProductResultsListener=mProductResultsListener;
}
Then I was able to access the method and not getting NPE. thanks all

Related

How to pass data from a activity to a recycler view adapter in android

I'm trying to design a page where address are stored in recycler view -> cardview.
When the user clicks the add address button from the Activity A the user is navigated to the add address page in Activity B. Here the user can input customer name, address line 1 and address line two.
And once save button is clicked in Activity B, a cardview should be created under the add address button in the Activity A.
This design is just like the amazon mobile app add address option.
Could anyone give me an example hoe to pass the saved data from activity to recycler adapter. I know how to pass data from recycler adapter to activity with putExtra etc..
Kindly help me. Million Thanks in advance!
Code In Activity A(Where the Add address button is available and where the recycler view is present)
public class ProfileManageAdressFragment extends AppCompatActivity {
RecyclerView recyclerView;
ProfileManageAddressRecyclerAdapter adapter;
ArrayList<ProfileManageAddressGetterSetter> reviews;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_profile_manage_adress);
Button addAddress = findViewById(R.id.addNewAddress);
reviews = new ArrayList<>();
addAddress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
startActivity(intent);
}
});
}
}
Piece of Code that is responsible for adding a card view in Activity A. Kindly let me know how to invoke this below code on button click in Activity
reviews.add(new ProfileManageAddressGetterSetter("Customer Name", "address line 1", "address line 2"));
recyclerView = findViewById(R.id.addressRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
adapter = new ProfileManageAddressRecyclerAdapter(this, reviews);
recyclerView.setAdapter(adapter);
Code in the Recycler adapter
public class ProfileManageAddressRecyclerAdapter extends RecyclerView.Adapter<ProfileManageAddressRecyclerAdapter.ViewHolder> {
private ArrayList<ProfileManageAddressGetterSetter> mDataset = new ArrayList<>();
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView customer_name, address_one, address_two;
private Button edit, remove;
public ViewHolder(View v) {
super(v);
customer_name = (TextView) v.findViewById(R.id.customerName);
address_one = (TextView) v.findViewById(R.id.addressLineOne);
address_two = v.findViewById(R.id.addressLineTwo);
}
}
public ProfileManageAddressRecyclerAdapter(View.OnClickListener profileManageAdressFragment, ArrayList<ProfileManageAddressGetterSetter> dataset) {
mDataset.clear();
mDataset.addAll(dataset);
}
#Override
public ProfileManageAddressRecyclerAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_manage_address, parent, false);
ProfileManageAddressRecyclerAdapter.ViewHolder vh = new ProfileManageAddressRecyclerAdapter.ViewHolder(view);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ProfileManageAddressRecyclerAdapter.ViewHolder holder, int position) {
ProfileManageAddressGetterSetter profileManageAddressGetterSetter = mDataset.get(position);
holder.address_one.setText(profileManageAddressGetterSetter.getAddress_line_1());
holder.address_two.setText(profileManageAddressGetterSetter.getGetAddress_line_2());
holder.customer_name.setText(profileManageAddressGetterSetter.getContractor_name());
}
#Override
public int getItemCount() {
return mDataset.size();
}
}
enter image description here - After trying the call from adapter using intent as mentioned above ended up with a 0.
Code in the Activity B
public class AddNewAddress extends AppCompatActivity {
private EditText customer_name, address_one, address_two;
private TextView cancel;
private Button add_address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_address);
customer_name = findViewById(R.id.customerName);
address_one = findViewById(R.id.addressOne);
address_two = findViewById(R.id.addressTwo);
add_address = findViewById(R.id.addAddress);
cancel = findViewById(R.id.completeCancel);
String cancel_text = "Cancel";
SpannableString spanableObject = new SpannableString(cancel_text);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(AddNewAddress.this, "Clicked", Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
}
};
spanableObject.setSpan(clickableSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
cancel.setText(spanableObject);
cancel.setMovementMethod(LinkMovementMethod.getInstance());
final ProfileManageAdressFragment profileManageAdressFragment = new ProfileManageAdressFragment();
add_address.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(AddNewAddress.this, ProfileManageAdressFragment.class);
startActivity(intent);
}
});
}
private void setFragment(android.support.v4.app.Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment).commit();
}
}
Update 1:
Kindly check my updated Recycler adapter. When I run this 0 is displayed in the text area as shown in the attached image. I'm new to android. Kindly help with example.
I finally achieved my goal with the use of ActivityResult. Now I'm able to pass data from Activity to Cardview.
Solution: When button is clicked in Activity A, I start the activity with startResultActivity(). Later, when the Activity B i triggered. The end-user inputs the data and that data is passed with the use of putExtra() and once the save button is clicked in Activity B next setResult() in Activity B and finish().
Finally i define onActivityResult() in Activity A to get the result. Works well!!
I would create a global variable and then store all the data in that variable and simply just call that variable in adapter.
declare a global variable and assign null value to it:
public static String checking = null;
a then store data in when you need it:
checking = check.getText().toString();
then call it in your adapter class.
first make interface listener inside listener make function with parameter like this
interface YourRecycleViewClickListener {
fun onItemClickListener(param1:View, param2: String)
}
now extend your activity
class YourActivity:YourRecycleViewClickListener{
override fun onItemClickListener(param1:View, param2: String) {
//do any thing
}
}
third step make interface constract in your recycle adapter
class YourAdapter(
private val listener: YourRecycleViewClickListener){
holder.constraintLayout.setOnClickListener{
listener.onItemClickListener(param1,param2)
}
}
this is by kotlin lang
and by java is same but change syntax
that all to do

RecyclerView - Get the selected position and pass the position to another activity where the same list will appear with that specific item selected

I have declared a ViewModel.class for the RecyclerView an Adapter and I have parsed to the MainActivity and to another Activity so I have the same adapter for both activities.
I can show the parsed data in both activities but the problem it is I cannot take the data of the selected item to MainActivity and then to set to that btnSearch for a click so then I can share between activities the data from the selected item.
Every time when the app is open the first item is selected. What I am trying to achieve is.
Get the item position to the MainActivity so when I click for a button search the data of the selectedItem will going to intent.putExtra and then get the data at another Activity.
If I click the second item and go to another Activity the same item will be selected.
Here is what I have tried so far.
The SearchEngineAdapter.class
public class SearchEngineAdapter extends RecyclerView.Adapter<SearchEngineAdapter.ViewHolder> {
private int selectedItem = 0;
private static RecyclerViewClickListener itemListener;
private Context context;
ArrayList<SearchEngine> arrayList = new ArrayList<>();
public SearchEngineAdapter(Context context, ArrayList<SearchEngine> arrayList, int selectedItem) {
this.context = context;
this.arrayList = arrayList;
this.selectedItem = selectedItem;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int i) {
holder.tvIcon.setImageResource(arrayList.get(i).getIcon());
holder.tvId.setText(arrayList.get(i).getId());
holder.tvSearchUrl.setText(arrayList.get(i).getUrl());
final String url = holder.tvSearchUrl.getText().toString();
SharedPreferences sp = context.getSharedPreferences("SavedSelected", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("selected", selectedItem);
editor.apply();
sp = context.getSharedPreferences("SavedSelected", Context.MODE_PRIVATE);
int myIntValue = sp.getInt("selected", -1);
Log.d("Selected", "SharedPreferences" + myIntValue);
if (selectedItem == i) {
holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
Intent intent = new Intent("search_engines");
intent.putExtra("url", url);
intent.putExtra("selected", selectedItem);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else {
holder.tvIcon.setBackgroundColor(Color.parseColor("#00000000"));
}
holder.tvIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("search_engines");
intent.putExtra("url", url);
int PreviousSelectedItem = selectedItem;
selectedItem = i;
intent.putExtra("selected", selectedItem);
holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
notifyItemChanged(PreviousSelectedItem);
notifyDataSetChanged();
}
});
}
// ... Other necessary functions.
}
Now the MainActivity.class
RecyclerView paramRecyclerView;
SearchEngineAdapter sEngineAdapter;
paramRecyclerView = findViewById(R.id.lvEngines);
paramRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
paramRecyclerView.setHasFixedSize(true);
Intent intent = getIntent();
int intValue = intent.getIntExtra("selected", 0);
sEngineAdapter = new SearchEngineAdapter(context, arrayList, intValue);
paramRecyclerView.setAdapter(sEngineAdapter);
// Calling network APIs to populate the arrayList.
The onResume function of the MainActivity looks like the following.
protected void onResume() {
super.onResume();
searchPlugin.setText("");
getChangeColor();
}
This is the click handler defined in MainActivity which send me to another Activity.
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newEntry = searchPlugin.getText().toString();
AddHistory(newEntry);
getFragmentRefreshListener().onRefresh();
Intent intent = new Intent(MainActivity.this, ActivitySearchEngine.class);
intent.putExtra("url", url );
intent.putExtra("name", newEntry);
intent.putExtra("selected", selectedItem2);
startActivity(intent);
}
});
And the other activity which is ActivitySearchEngine.class
public class ActivitySearchEngine extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
public String selectedName;
public int selectedID;
public String selectedSearchUrl;
RecyclerView mListView;
RecyclerView paramRecyclerView;
SearchEngineAdapter sEngineAdapter;
ArrayList<SearchEngine> arrayList = new ArrayList<>();
final Context context = this;
int selectedItem;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
// Variables initialization
// Setting up adapter
paramRecyclerView.setAdapter(sEngineAdapter);
sEngineAdapter.notifyDataSetChanged();
// Calling network APIs to populate the arrayList here
Intent receivedIntent = getIntent();
selectedName = receivedIntent.getStringExtra("name");
selectedID = receivedIntent.getIntExtra("id", 1); //NOTE: -1 is just the default value
selectedSearchUrl = receivedIntent.getStringExtra("url");
// Loading the url in a WebView for searching
}
}
I could share the selected item position between these two activities. However, I am not sure how to achieve the behavior of the item is being selected in the RecyclerView of the second activity as well. If I select another item in the RecyclerView of the second activity, the change should be reflected in the first (i.e. MainActivity) as well when I get back to it.
Any help would be appreciated.
There was a lot of changes in the question and hence the last update of this answer below is the final version.
As far as I could understand about the problem, I can see you are very close to the solution if I had understood correctly. The SearchEngineAdapter already has a selectedItem variable in it which can be used for highlighting the item selected in ActivitySearchEngine as well. You just have to modify the adapter a little bit like the following. I am rewriting the adapter here.
public class SearchEngineAdapter extends RecyclerView.Adapter<SearchEngineAdapter.ViewHolder> {
private int selectedItem = 0;
private static RecyclerViewClickListener itemListener;
private Context context;
ArrayList<SearchEngine> arrayList = new ArrayList<>();
// Added another argument to be passed in the constructor
public SearchEngineAdapter(Context context, ArrayList<SearchEngine> arrayList, int selectedItem) {
this.context = context;
this.arrayList = arrayList;
this.selectedItem = selectedItem;
}
#NonNull
#Override
public SearchEngineAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.s_engine_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int i) {
if (selectedItem == i) {
holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
} else {
holder.tvIcon.setBackgroundColor(Color.parseColor("#00000000"));
}
holder.tvIcon.setImageResource(arrayList.get(i).getIcon());
holder.tvId.setText(arrayList.get(i).getId());
holder.tvSearchUrl.setText(arrayList.get(i).getUrl());
holder.tvIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int PreviousSelectedItem = selectedItem;
selectedItem = i;
holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
notifyItemChanged(PreviousSelectedItem);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tvId, tvSearchUrl;
ImageView tvIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvId = itemView.findViewById(R.id.ivEngineText);
tvIcon = itemView.findViewById(R.id.ivEngine);
tvSearchUrl = itemView.findViewById(R.id.ivSearchUrl);
}
}
}
Check that, I just have modified the constructor of your adapter, taking another extra variable which is selectedItem. Just pass the selected item position when you are initializing the adapter in both activities. In the default case, you can pass -1, I think you get the idea.
You have passed the selected item position to the ActivitySearchEngine as well. Which can be used for initializing for the desired behavior. Hope that helps!
Update 1:
I would like to suggest you put the following code to your onResume function in the ActivitySearchEngine class. You might consider removing the lines from the onCreate function of your code as well.
#Override
public void onResume() {
super.onResume();
Intent receivedIntent = getIntent();
selectedName = receivedIntent.getStringExtra("name");
selectedID = receivedIntent.getIntExtra("id", 1); // NOTE: -1 is just the default value
selectedSearchUrl = receivedIntent.getStringExtra("url");
sEngineAdapter = new SearchEngineAdapter(context, arrayList, selectedID);
paramRecyclerView.setAdapter(sEngineAdapter);
}
Update 2:
The RecyclerView in your MainActivity is getting reloaded as you are setting the adapter again to the RecyclerView in the onResume function. Moreover, you are trying to get data from intent which is not available here I think because you have not set any data to be sent to the MainActivity when you return back from the ActivitySearchEngine. Hence, the RecyclerView is reloading again with a fresh set of data.
You might remove the code associated with your RecyclerView from the onResume function of the MainActivity to remove this complication as I think this is not necessary. So the updated onResume function will look like the following.
protected void onResume() {
super.onResume();
searchPlugin.setText("");
getChangeColor();
}
Update 3:
Take a public static variable in your MainAcitivity and declare it as a global variable like the following.
// Setting 0 as you wanted to put the first item at the first time
// If you do not want that, then initialize with -1
public static int selectedItem = 0;
Now inside your onCreate function, remove the lines for getting the intent.
paramRecyclerView = findViewById(R.id.lvEngines);
paramRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
paramRecyclerView.setHasFixedSize(true);
// Remove the following
// Intent intent = getIntent();
// int intValue = intent.getIntExtra("selected", 0);
// Move the adapter setup to the onResume
// sEngineAdapter = new SearchEngineAdapter(context, arrayList, selectedItem);
// paramRecyclerView.setAdapter(sEngineAdapter);
// Calling network APIs to populate the arrayList.
Modify the onResume function in the MainActivity to set up the adapter there.
protected void onResume() {
super.onResume();
searchPlugin.setText("");
getChangeColor();
// Set the adapter here
sEngineAdapter = new SearchEngineAdapter(context, arrayList, selectedItem);
paramRecyclerView.setAdapter(sEngineAdapter);
}
Modify the onClickListener in your adapter like the following. Just add a new line there.
holder.tvIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("search_engines");
intent.putExtra("url", url);
int PreviousSelectedItem = selectedItem;
selectedItem = i;
// Set the static value in the MainActivity
// This can be accessed from all other classes
MainActivity.selectedItem = i;
intent.putExtra("selected", selectedItem);
holder.tvIcon.setBackgroundColor(Color.parseColor("#30000000"));
notifyItemChanged(PreviousSelectedItem);
notifyDataSetChanged();
}
});
Hope that helps!

Click button in one tab and change textview in the anoter tab

I have tab1 and tab3 also these have their classes and I want to click button in tab1 and change textview in the tab3, but I couldn't find anyway.
This is my tab1 class
public class tab1Contacts extends Fragment{
TextView tv;
EditText et;
TextView tv3;
personInfo pı;
public personInfo returnpı(){
return pı;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1contents, container, false);
Button btn_jog = (Button) rootView.findViewById(R.id.jogging_button);
tv = (TextView) rootView.findViewById(R.id.newRecordText);
et = (EditText) rootView.findViewById(R.id.durationtext) ;
pı = new personInfo();
pı.eyesPower = 100;
pı.brainPower = 100;
pı.armsPower = 100;
pı.legsPower = 100;
pı.hearthPower = 100;
pı.energyLevel = 100;
pı.calorie = 2000;
pı.condition = 0;
btn_jog.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int duration = Integer.parseInt(et.getText().toString());
pı.jogging(duration);
//I want to change here textview in the tab3.
}
});
return rootView;
}
}
This also my tab3 Class:
public class Tab3Contacts extends Fragment {
TextView tv3;
double newBrainpower;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3contents, container, false);
tv3 = (TextView) rootView.findViewById(R.id.list_text) ;
return rootView;
}
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pager.setCurrentItem(yourindex);// if you use pager
getTabHost().setCurrentTab(yourindex);
}
});
If I'm reading your question correctly, then what you need is for tab3 to listen to events from tab1. For that you will want to implement some kind of internal notification/eventing system. This is typically handled through a notification handling class that will register observers/listeners.
An example from a project I've been maintaining:
public class NotificationManager {
public interface Observer {
public void update(String notificationName, Bundle data);
}
private static NotificationManager singletonNotifier = null;
private HashMap<String, ArrayList<Observer>> mObservables = null;
private NotificationManager() {
mObservables = new HashMap<String, ArrayList<Observer>>();
}
//enforce singleton
public static NotificationManager getInstance() {
if (singletonNotifier == null) {
singletonNotifier = new NotificationManager();
}
return singletonNotifier;
}
public void addObserver(String notificationName, Observer observer) {
// add to map
// in multi-threaded apps make sure you use synchronized or a mutex
}
public void removeObserver(String notificationName, Observer observer) {
// remove from map; mind threading
// overload as necessary for your design
}
public void notifyObservers(String notificationName, Bundle data) {
// go through your map of observers, build an array of observers
// that need to update, then for each observer, call
// observer.update(notificationName, data);
}
}
Then your tab3 class would need to implement the Observer interface and on object construction register itself with the NotificationManager with a string value for the type of notification it wants (use best practices for constants instead of string literal arguments), using the call
NotificationManager.getInstance().addObserver("Tab1DataChange", this);
It will need to implement the update(String, Bundle) method, which will make all the changes that you need.
Then in the class for the tab1 object, add to the click listener this call:
NotificationManager.getInstance().notifyObservers("Tab1DataChange", data);
Where data is any information that observers would need to know to respond. In keeping with the idea of decoupling code, do not put together a data bundle that is explicitly for one listener, because at some point you might need something else to listen for the same event. Save yourself some grief now by designing the data bundle to contain what would need to update regardless of who is consuming the event.
Some lessons learned for me:
Pay attention to Android lifecycle. OnPause and OnDestroy for the active view(s) should unregister the listener so that you don't end up with a null pointer exception if something triggers that event while the observer object is not available. OnCreate and OnResume should reregister. In some cases I have been able to not worry about OnPause/OnResume, but depending on your app you may need them.

String doesn't update when passed to different Fragment?

I'm trying to send two strings from Fragment A to Fragment B. Currently, i've implemented an interface listener like this...
Fragment A Method:
String arrayTitle1 = arrayList.get(Index).get("ArrayTitle1");
String arrayTitle2 = arrayList.get(Index).get("ArrayTitle2");
((TextFooterListener) getActivity()).onTextFooterListener(arrayTitle1, arrayTitle2);
I pass these strings to the Activity which sends them to Fragment B like this...
Activity:
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.arrayTitle1 = arrayTitle1;
FragmentB.arrayTitle2 = arrayTitle2;
}
Fragment B:
They are then received and stored in Fragment B as public strings.
public String arrayTitle1;
public String arrayTitle2;
And in the onCreateView of Fragment B, i try to assign these strings.
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
But unfortunately, whenever Fragment A's method gets called, the onTextFooterListener doesn't seem to update the strings.
Any suggestions?
You change the values in Fragment B but never actually reassign the new values into the TextViews.
Change
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.setTitles(arrayTitle1, arrayTitle2);
}
And in Fragment B :
public void setTitles(String title1, String title2) {
arrayTitle1 = title1;
arrayTitle2 = title2;
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
}
There is a mechanism you need to use to pass data to a Fragment. Please consider the following approach:
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
Bundle data = new Bundle();
data.putString("array1", arrayTitle1);
data.putString("array2", arrayTitle2);
FragmentB b = FragmentB();
b.setArguments(data);
}
Then in your onCreateView of FragmentB:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
arrayTitle1 = getArguments().getString("array1");
arrayTitle2 = getArguments().getString("array2");
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
return inflater.inflate(R.layout.fragment, container, false);
}

Android: I'm confused about how to get the right context to find my Views

I've got my MainTabbedActivity.xml which holds a Fragment.xml.
Fragment.xml holds a ListView.
ListView gets populated with an array of CustomView.xml.
CustomView.xml holds a TextView which I wanna pass a string to.
So it's like:
MainTabbedActivity.xml
-Fragment.xml
--CustomView.xml
I create the new CustomViews inside of Fragment.java and populate the ListView with them. This is working fine. But as soon as I try to find my TextView to set the Text, it crashes because it is NULL. I'm pretty new to Android, but after a day of googleing it looks like I'm passing the wrong context. I just don't know how to get the right one.
This is how I pass the context in Fragment.java:
customView newCV = new customView (getActivity.getApplicationContext());
newCV.setName("HELLO");
Here's what my CustomView.java looks like, including the line where it returns null:
public class customView extends FrameLayout{
String name;
//Views
TextView userNameToSet;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public customView(Context context){
super(context);
userNameToSet = (TextView)findViewById(R.id.userName); //this always returns NULL...
userNameToSet.setText(getName()); //...and this crashes because of it
}
}
Thanks for any help :)
EDIT: Here is the Fragment.java which creates the CustomViews
public class StatusFragment extends Fragment {
ListView listView;
customView cvCollection[];
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static StatusFragment newInstance(int sectionNumber) {
StatusFragment fragment = new StatusFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public StatusFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status_tabbed, container, false);
//Get Statuslist from Server
getStatusListFromServer(rootView);
return rootView;
}
private void getStatusListFromServer(final View inflatedView){
//JSON SERVICE *********************************
String json = "{'userID' : '1'}";
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder()
.url("example.com")
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
}
#Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
try {
String jsonString = response.body().string();
JSONArray jArray = new JSONArray(jsonString);
cvCollection = new customView[jArray.length()];
for (int i=0; i<jArray.length() -1; i++){
final JSONObject statusRow = jArray.getJSONObject(i);
customView newCV = new customView(inflatedView.getContext());
newCV.setName(statusRow.getString("userID"));
newCV.setStatus(statusRow.getString("text"));
newCV.setErstellt(statusRow.getString("erstellt"));
newCV.setFavorite(false);
cvCollection[i] = newCV;
}
//Put them into my List
fillList(inflatedView, cvCollection);
}catch(JSONException ex){
}
//Log.w("JSON***************", response.body().string());
} else{
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
}
public void fillList(View inflatedView, customView[] statsList){
listView = (ListView) inflatedView.findViewById(R.id.statusList);
//Get my stats
customView customView_data[] = statsList;
//Load them into the adapter
customViewAdapter adapter = new customViewAdapter(getActivity(), R.layout.customView, customView_data);
//Load adapter into ListView
listView.setAdapter(adapter);
}
}
}
Your code is not complete, so I'm not sure, but I think you need to inflate your custom view somewhere. From what I see, you're not doing that.
Something like:
View view= getLayoutInflater().inflate(R.layout.CustomView, null);
userNameToSet = (TextView)view.findViewById(R.id.userName);
As far as I know, findViewById() can be called from any activity, in that case it will look for the View inside the layout set with setContentView().
Otherwise you should call it from the layout that contains the view.
Usually you dont need to pass the Context between your activity/fragment classes.
If your View is loaded at the parent Activity then you should call
getActivity().findViewById()
If your View is inside your fragment you must call it from the container view, usually inside of
onCreateView(LayoutInflater, ViewGroup, Bundle) //Cant see this method in the code you posted
Right before you inflate your layout.
Hope this helps.
thank you all for trying to help me!
I just couldn't get it to work the way I set it up, so I created an Adapter for my custom listView items. Now it works perfectly!
If anyone has a similar problem, please check out this tutorial which helped me: http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

Categories