display user information in fragment - java

I'm using Android Studio 3.5.
I built bottom navigation with fragment and then I'm trying to display user information such as (name - email - image and so on) that is retrieved from a Firebase store .. when reach to :
documentReference.addSnapshotListener (this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot,
#Nullable FirebaseFirestoreException e) {
t_name.setText(documentSnapshot.getString("name"));
t_email.setText(documentSnapshot.getString("email"));
}
});
It gave me a red line under the code... When click Alt + Enter, it gives me solve put cast (Executor) before (this) after the cast the app doesn't work ...
I don't know how to solve it ..
Note: This is on fragment not in activity
And if I want to display image that retrieve from Firebase... How can I do it?
Thanks
account Fragment
public class accountFragment extends Fragment {
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
TextView logout_account , t_name , t_email , t_country ;
public accountFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_account, container, false);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
userID = fAuth.getCurrentUser().getUid();
t_country = v.findViewById(R.id.t_country);
t_email = v.findViewById(R.id.t_email);
t_name = v.findViewById(R.id.t_name);
DocumentReference documentReference = fStore.collection("eng.users").document(userID);
documentReference.addSnapshotListener (this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot,
#Nullable FirebaseFirestoreException e) {
t_name.setText(documentSnapshot.getString("name"));
t_email.setText(documentSnapshot.getString("email"));
t_country.setText(documentSnapshot.getString("country"));
}
});
}
Activity_account.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".categories.accountFragment">
<LinearLayout
android:id="#+id/banner_pro"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/banner2"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingTop="10dp">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/mypic"
app:civ_border_color="#color/colorDarkBlue"
app:civ_border_width="2dp" />
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dp"
>
<TextView
android:textColor="#color/colorDarkBlue"
android:id="#+id/t_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/t_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:text="Email"
android:textSize="15dp" />
<TextView
android:id="#+id/t_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="country"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:paddingLeft="20dp"
android:id="#+id/logout_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/banner_pro"
android:layout_marginTop="20dp"
android:text="LogOut"
android:textSize="30dp" />
</RelativeLayout>

The problem is on
documentReference.addSnapshotListener (this, ...
use
documentReference.addSnapshotListener (getActivity(), ...
You are in a fragment

Related

Showing created data from Activity in RecyclerView in a child fragment of TabLayout

I have a nav fragment section called GroupbookFragment which uses a TabLayout with 3 Tabs.
(GroupbookKrippeFragment, GroupbookHafenFragment & GroupbookKindergartenFragment)
Each Tab has a CardView with a RecyclerView in it. The goal is to have a global "Add Child" Button on the GroupbookFragment level, which forward you to another activity with a form where you can create a new child. After filling data & clicking a save button, you'll come back to the GroupbookFragmentview and the new created data from the form will show up in the correct CardView within one of the three corresponding fragments.
So far I successfully implemented all functionalities when the methods and "Add child" button are already IN the corresponding tab child fragment (e.g. GroupbookKrippeFragment). The problem here is, whenever I change the tab from the tabLayout, the "Add Child" button is moving away aswell. So after moving all methods & the button into the parent fragment GroupbookFragment I can navigate through all pages and fill the form & save, but the recyclerView won't update and show the new created data in the child's recyclerView anymore?!
Thanks in advance for any help! Here is the code view:
GroupbookFragment.java
public class GroupbookFragment extends Fragment {
// initializing elements
RecyclerView recyclerView;
ChildListAdapter childListAdapter;
List<Child> children = new ArrayList<>();
RoomDb database;
FloatingActionButton addChildBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_groupbook, container, false);
// inflate the layout for GB Krippe fragment
View viewKrippe = inflater.inflate(R.layout.fragment_groupbook_krippe, container, false);
// link recycler view to correct element
recyclerView = viewKrippe.findViewById(R.id.recycler_view_krippe);
// Tab layout initialization
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
GroupbookAdapter adapterGroupBook = new GroupbookAdapter(getChildFragmentManager(), getLifecycle());
viewPager.setAdapter(adapterGroupBook);
// set 3 Tab titles
tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));
tabLayout.addOnTabSelectedListener(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.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add Child code
database = RoomDb.getInstance(getContext());
children = database.mainDAO().getAll();
updateRecycler(children);
// link var to app element & set on click route
addChildBtn = view.findViewById(R.id.child_add_btn);
addChildBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupbookFragment.this.getContext(), AddChildActivity.class);
startActivityForResult(intent, 101);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Child new_child = (Child) data.getSerializableExtra("child");
database.mainDAO().insert(new_child);
children.clear();
children.addAll(database.mainDAO().getAll());
childListAdapter.notifyDataSetChanged();
}
}
}
void updateRecycler(List<Child> children) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(GroupbookFragment.this.getContext()));
childListAdapter = new ChildListAdapter(GroupbookFragment.this.getContext(), children, childClickListener);
recyclerView.setAdapter(childListAdapter);
}
private final ChildClickListener childClickListener = new ChildClickListener() {
#Override
public void onClick(Child child) {
}
#Override
public void onLongClick(Child child, CardView cardView) {
}
};
}
GroupbookFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorAnimationMode="elastic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tabLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager_groupBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- Add floating button to add child-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/child_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="#dimen/margin_cardView"
android:backgroundTint="#color/green"
android:src="#drawable/ic_add_child"
app:tint="#color/white" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
GroupbookKrippeFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Groupbook.GroupbookKrippeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/groupBook_krippe_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/margin_cardView"
app:cardCornerRadius="#dimen/cornerRadius_Card"
app:cardElevation="#dimen/elevation_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/groupBook_krippe_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/creme_500"
android:layout_weight="6"
android:padding="#dimen/padding_default">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline_group_1_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<ImageView
android:id="#+id/ic_groupBook_krippe_1"
android:layout_width="#dimen/iconSize_groupBook_header"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_groupbook_krippe_1_logo"
app:tint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline_group_1_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/title_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gruppe 1"
android:textAllCaps="true"
android:textSize="#dimen/textSize_groupBook_title"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/desc_groupBook_krippe_1"/>
<TextView
android:id="#+id/desc_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gesamt: 12 | Anwesend: 10"
android:textSize="#dimen/textSize_groupBook_subTitle"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/title_groupBook_krippe_1"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="#dimen/padding_default"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="ID"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Vorname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nachname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtstag"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtsort"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Konfession"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nationalität"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
</TableRow>
</TableLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_krippe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>

my app in android studio is crashing when i come to the part recyclerView.setAdapter(adapter);

I am trying to fetch some data in android studio from my online database backendless.com but the proplem is not with the online serves it is when i try to inflate the data. i dont know is it in the layaot file or in the java class
this is my java activitiy ViewMyBusiness.java
public class ViewMyBusiness extends AppCompatActivity {
ListView recyclerView;
private View mProgressView;
private View mLoginFormView;
private TextView tvLoad;
ContactsAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_my_business);
recyclerView = findViewById(R.id.recycler_view);
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
tvLoad = findViewById(R.id.tvLoad);
String whereClause = "userEmail = '" + BackendlessCall.user.getEmail() + "'";
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause(whereClause);
queryBuilder.setGroupBy("companyName");
showProgress(true);
Backendless.Persistence.of(Contact.class).find(queryBuilder, new AsyncCallback<List<Contact>>() {
#Override
public void handleResponse(List<Contact> response) {
adapter = new ContactsAdapter(ViewMyBusiness.this, response);
//the crash is here
recyclerView.setAdapter(adapter);
showProgress(false);
}
#Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(ViewMyBusiness.this, "ERROR: " + fault.getMessage(), Toast.LENGTH_SHORT).show();
showProgress(false);
}
});
}
this is the contacts adapter java class
public class ContactsAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contacts;
public ContactsAdapter(Context context, List<Contact> list)
{
super(context, R.layout.row_layout, list);
this.context = context;
this.contacts = list;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, parent, false);
TextView tvChar = convertView.findViewById(R.id.tvChar);
TextView tvCName = convertView.findViewById(R.id.tvCName);
TextView tvBField = convertView.findViewById(R.id.tvBField);
tvChar.setText(contacts.get(position).getCompanyName().toUpperCase().charAt(0) + "");
tvCName.setText(contacts.get(position).getCompanyName());
tvBField.setText(contacts.get(position).getBusinessField());
return convertView;
}
}
the layout for the listview in activity_view_my_business.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ViewMyBusiness">
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:visibility="gone" />
<TextView
android:id="#+id/tvLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:clickable="true"
android:focusable="true"
android:gravity="center_horizontal"
android:text="Loading...please wait..."
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/login_form"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Business"
android:textSize="24sp"
android:textStyle="bold" />
<ListView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/row_layout"/>
</LinearLayout>
</LinearLayout>
this is the view file that will inflate when data is passed
the row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/List"
android:orientation="horizontal">
<TextView
android:id="#+id/tvChar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="A"
android:textColor="#color/ListText"
android:textSize="48sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:orientation="vertical">
<TextView
android:id="#+id/tvCName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#color/ListText"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvBField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="TextView"
android:textColor="#color/ListText" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You are getting NullPointerException in that line, becasue in this line -
recyclerView = findViewById(R.id.recycler_view);
you are assigning null value to recyclerView variable !
It's because in the activity layout file you have defined a list view not RecyclerView, change this part
<ListView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/row_layout"/>
to this part of xml code -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/row_layout"/>
and to use recyclerView you have to add these -->
implementation 'com.android.support:recyclerview-v7:28.0.0' ,
dependency to your build.gradle (app:module) file
and here is official documentation about RecyclerView - Andorid RecyclerView
Enjoy!!

Using Custom DialogFragment as dynamic Dialog Box android-java

I created a custom dialog fragment to be used as a YesNo Dialog Box for my project as shown below
I want to use it dynamically in where I can use it not only for one xml file.
Does anybody know how can I set the button click listener in the appcompact class from the dialogfragment on call?
I just want to set the buttonclick event of the dialogfragment in another xml file.
I tried using interface but it gives me a null pointer exception so I tried the code below.
the YesNoDialogFragment Class
public class DialogYesNo extends DialogFragment {
LayoutInflater inflater;
View v;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.dialog_yesno,null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);
return builder.create();
}
}
the XML file of the DialogFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/dialog_background"
android:layout_gravity="center">
<TextView
android:id="#+id/yesno_title"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="TITLE"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="10pt"
android:background="#1b1b01"
android:textColor="#fff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:paddingLeft="10dp"
android:textStyle="bold"/>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#520456">
<TextView
android:id="#+id/yesno_message"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Message"
android:textAllCaps="true"
android:textSize="12dp"
android:textColor="#fff"
android:paddingLeft="10dp"
android:textStyle="bold"/>
</ScrollView>
<View
android:id="#+id/divider1"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="?android:attr/listDivider" />
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center">
<Button
android:id="#+id/dialog_No"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="NO"
android:background="#drawable/dialog_background"
android:textColor="#000"
android:textStyle="bold"
android:textSize="10pt"/>
<Button
android:id="#+id/dialog_Yes"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="YES"
android:layout_marginLeft="8dp"
android:background="#drawable/dialog_background"
android:textColor="#000"
android:textStyle="bold"
android:textSize="10pt"/>
</LinearLayout>
this is the code in the MainActivity
btnYes = dialogYesNo.getActivity().findViewById(R.id.dialog_Yes);
btnNo = dialogYesNo.getActivity().findViewById(R.id.dialog_No);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,TutwithNavigation.class);
startActivity(intent);
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code here
}
});
try findViewById in your DialogYesNo, and add setListener for it
like this:
private OnClickListener onYesClick;
private OnClickListener onNoClick;
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.dialog_yesno,null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);
View btnYes = v.findViewById(R.id.dialog_Yes);
View btnNo = v.findViewById(R.id.dialog_No);
btnYes.setOnClickListener(onYesClick);
btnNo.setOnClickListener(onNoClick);
return builder.create();
}
}
public void setOnYesClick(OnClickListener listener){
onYesClick = listener
}
public void setOnNoClick(OnClickListener listener){
onNoClick = listener
}
use setOnYesClick and setOnNoClick instead

Android CustomListAdapter Setting TextView from EditText OnClick in TAB

On one of my tabs I have a Listview that I want to populate when a user enters text on the edittext and then presses a button. The edittext and the button are on the tabbed layout along with the listview.
The textview and imageview are set in the customlistadapter. But when I press the button nothing happens and I can't seem to work out why.
Tab3.java
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.tab3, container, false);
final EditText notes = (EditText) v.findViewById(R.id.editText);
listView=(ListView)v.findViewById(R.id.list);
Button button = (Button) v.findViewById(R.id.button3);
String note = notes.getText().toString();
int[] cross = new int[]{R.drawable.cross};
String [] notesofrules = new String[]{note};
final CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), notesofrules, cross);
listView=(ListView) v.findViewById(R.id.list);
Button rulesSet = (Button)v.findViewById(R.id.button3);
rulesSet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
listView.setAdapter(adapter);
}
});
return v;
}
CustomListAdaper:
public CustomListAdapter(Activity context, String[] notes, int[] imageCross) {
super(context, R.layout.item);
// TODO Auto-generated constructor stub
this.context=context;
this.notes = notes;
this.imageCross = imageCross;
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.item, null,false);
TextView ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
ImageView image = (ImageView) rowView.findViewById(R.id.icon);
image.setImageResource(imageCross[position]);
ruleNotesSet.setText(notes[position]);
return rowView;
}
tab3.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<TextView android:text="#string/thirdTabTitle" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF9900"
android:id="#+id/textView"
android:textIsSelectable="true"
android:textSize="55px"
android:shadowColor="#73ffffff"
android:fontFamily="sans-serif-bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="100px"
android:ems="10"
android:padding="10dp"
android:id="#+id/editText"
android:background="#drawable/edittext_rounded_corners"
android:layout_marginTop="18dp"
android:layout_below="#+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/addnote"
android:background="#drawable/calculate_button"
android:textColor="#000000"
android:fontFamily="sans-serif-bold"
android:layout_below="#+id/editText"
android:textSize="40px"/>
<TextView
android:id="#+id/notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="45px"
android:layout_below="#+id/button3"
android:fontFamily="sans-serif-bold"
android:layout_toRightOf="#id/autohighlight_checkbox"
/>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/notes" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4CBE99"
android:textSize="14dp"
android:text="Item"
android:textAllCaps="true"
android:textStyle="bold" />
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
I think your adapter is empty. You only need to set it once, then make a refresh function in your adapter you'll call in your activity's onClick
public void refresh(String[] notes, int[] imageCross) {
this.notes = notes;
this.imageCross = imageCross;
this.notifySataSetChanged();
}

New Activity Goes to Blank Screen

I'm pretty new to Android Studio and I'm having difficulty starting a new activity. I've triple checked my code and I can't figure out what my problem is. I've also google searched for a couple of hours but nobody seems to have had the same problem as me. Logcat isn't reporting a problem, but when I run the app, and click the button, the app goes to a blank screen. Please help!
public class OriginalFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_original, container, false);
return rootView;
}
}
public class MainFragment extends Fragment {
private AlertDialog mDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Sets up the about button
View aboutButton = rootView.findViewById(R.id.about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.about_title);
builder.setMessage(R.string.about_text);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
mDialog=builder.show();
}
});
View originalButton = rootView.findViewById(R.id.original_button);
originalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), OriginalActivity.class);
getActivity().startActivity(intent);
}
});
View pictureButton = rootView.findViewById(R.id.picture_button);
originalButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(getActivity(), PictureActivity.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
#Override
public void onPause(){
super.onPause();
if (mDialog != null)
mDialog.dismiss();
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<FrameLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:clipChildren= "false"
tools:context= "org.example.abstract_art.MainActivity">
<fragment android:id= "#+id/main_fragment"
class= "org.example.abstract_art.MainFragment"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
tools:layout= "#layout/fragment_main" />
</FrameLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/select_shapes_background"
tools:context="org.example.abstract_art.OriginalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Shapes"
android:id="#+id/textView"
android:textSize="#dimen/menu_text_size"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="org.example.abstract_art.OriginalFragment"
android:id="#+id/fragment"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
tools:layout="#layout/fragment_original" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu_background"
android:elevation="#dimen/elevation_high"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context=".org.example.abstract_art.MainFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/menu_space"
android:text="#string/long_app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="#dimen/menu_text_size"/>
<Button
android:id="#+id/original_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/original_label"/>
<Button
android:id="#+id/picture_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/picture_label"/>
<Button
android:id="#+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/about_label"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu_background"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context="org.example.abstract_art.OriginalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circles"
android:id="#+id/circlesText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rectangles"
android:id="#+id/rectanglesText"
android:layout_below="#+id/circlesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squares"
android:id="#+id/squaresText"
android:layout_below="#+id/rectanglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triangles"
android:id="#+id/trianglesText"
android:layout_below="#+id/squaresText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4 Point Stars"
android:id="#+id/fourStarText"
android:layout_below="#+id/trianglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 Point Stars"
android:id="#+id/sixStarText"
android:layout_below="#+id/fourStarText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Art!"
android:id="#+id/createOriginalButton"
android:layout_below="#+id/sixStarText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please Make sure Below Things
You have Define Main Activity, OriginalActivity,PictureActivity in your Manifest file
Please Make sure You also set Layout in all Activity or Fragment and Bind Components Of All Layout in that class
try below code
Button originalBtn; //Define class Level
originalBtn=(Button)rootView.findViewById(R.id.original_button);
Now you can set click Listener and code in it
Second main things that you define two times originalButton Click Listener so i think thats why you get problem
I think second Listener should be pictureButton Click Listener

Categories

Resources