I have a fragment that contains a listview. When I press the position 0 of my listview I want to open another fragment but I can't call it. I think there is a problem with the managerFragment but I'm not sure.
Here is my code from Frm_principal that contains the listview in position 0
I want to call frmCliente
`package com.example.programacion.ventasje.Principal;
import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.internal.widget.AdapterViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.programacion.ventasje.Cliente.Frm_Cliente;
import com.example.programacion.ventasje.R;
import java.util.ArrayList;
/**
* Created by Programacion on 31/07/2015.
*/
public class Frm_principal extends Fragment {
TextView tv_funcion,tv_descripcion;
ImageView img_principal;
ListView listview_principal;
ArrayAdapter<Principal> adapter;
Principal dato;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_frm_principal,container,false);
inicializarComponentesUi(rootView);
inicializarListaContactos();
inicializarDatosLista();
listview_principal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0) {
Fragment newFragment = new Frm_Cliente();
FragmentTransaction asd = getActivity().getSupportFragmentManager().beginTransaction();
asd.replace(R.layout.layout_frm_principal,)
}
}
});
return(rootView);
}
private void inicializarDatosLista() {
Principal nuevo = new Principal("Cliente","AƱadir modificar o anular clientes",getResources().getDrawable(R.drawable.ic_cliente));
adapter.add(nuevo);
}
private void inicializarListaContactos() {
adapter = new PrincipalAdapter(getActivity(),new ArrayList<Principal>());
listview_principal.setAdapter(adapter);
}
private void inicializarComponentesUi(final View view) {
tv_funcion = (TextView)view.findViewById(R.id.tv_funcion);
tv_descripcion = (TextView)view.findViewById(R.id.tv_descripcion);
img_principal =(ImageView)view.findViewById(R.id.img_principal);
listview_principal =(ListView)view.findViewById(R.id.listview_principal);
}
}
`
and here is the frm cliente class that i want to call
package com.example.programacion.ventasje.Cliente;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.programacion.ventasje.R;
/**
* Created by Programacion on 30/07/2015.
*/
public class Frm_Cliente extends Fragment {
private TextView tvCliente,tvCentro,tvOficio,tvLocalidad;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_frmcliente,container,false);
inicializarComponentesUi(rootView);
return(rootView);
}
private void inicializarComponentesUi(final View view) {
tvCliente = (TextView)view.findViewById(R.id.tv_cliente);
tvCentro = (TextView)view.findViewById(R.id.tv_centro);
tvOficio = (TextView)view.findViewById(R.id.tv_oficio);
tvLocalidad = (TextView)view.findViewById(R.id.tv_localidad);
}
}
I think you forgot to commit the transaction.
Inside your onCLickListener
Frm_Cliente newFragment = new Frm_Cliente();
FragmentTransaction asd = getActivity().getSupportFragmentManager().beginTransaction();
asd.replace(R.layout.layout_frm_principal,newFragment)
ads.commit();
Related
this is my first android project.
I am making a login and register page for a game i am making, and i'm trying to figure the login/register stuff. my project so far consists of MainActivity.java, LoginFragment and a RegisterFragment.
my question is after declaring the username, password, loginbtn variables how should my Intent intent declaration look like?
note aside, my mainActivity consists of a viewPager which just lets me slide through the login and register pages. but i will supply the code below
mainActivity.java
package com.example.my_app_2;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.app.Dialog;
import android.content.Intent;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create instance of SQLite Database
ViewPager viewPager = findViewById(R.id.viewPager);
AuthenticationPagerAdapter pagerAdapter = new AuthenticationPagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragmet(new LoginFragment());
pagerAdapter.addFragmet(new RegisterFragment());
viewPager.setAdapter(pagerAdapter);
}
static class AuthenticationPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentList = new ArrayList<>();
public AuthenticationPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Override
public int getCount() {
return fragmentList.size();
}
void addFragmet(Fragment fragment) {
fragmentList.add(fragment);
}
}
}
LoginFragment:
package com.example.my_app_2;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
EditText username, password;
Button loginbtn;
public LoginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container,false);
username = v.findViewById(R.id.username);
password = v.findViewById(R.id.password);
loginbtn = v.findViewById(R.id.loginbtn);
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String usernameValue = username.getText().toString();
String passwordValue = password.getText().toString();
// i assume the Intent intent goes here, but i have no idea what arugments/parameters pass in the new Intent() part of it
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
and same goes with the registerFragment
package com.example.my_app_2;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* A simple {#link Fragment} subclass.
*/
public class RegisterFragment extends Fragment {
EditText username, password, email;
Button signupbtn;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_register, container,false);
username = v.findViewById(R.id.username);
password = v.findViewById(R.id.password);
email = v.findViewById(R.id.email);
signupbtn = v.findViewById(R.id.signupbtn);
signupbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String usernameValue = username.getText().toString();
String passwordValue = password.getText().toString();
String emailValue = email.getText().toString();
Intent intent = new Intent();
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
}
what i have tried to do...
what i am trying to do is just take the username and password and store them in sharedPreferences.
thanks in advance, i apoligize for the silliness of the question but any help would be appreciated!
i have tried Intent intent = new Intent(this, RegisterFragment); // ??????
honestly im kinda clueless lol xd
(RegisterFragment.this); // ???????
So this is my code for the fragment:
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class Frag1 extends Fragment {
CustomViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag1_layout, container, false);
viewModel = new ViewModelProvider(this).get(CustomViewModel.class);
RecyclerView recyclerView = rootView.findViewById(R.id.rv1);
final Adapter adapter = new Adapter(new Adapter.Diff());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
viewModel.show().observe(getViewLifecycleOwner(), rEnts -> {
adapter.submitList(rEnts);
Log.d("ItemCount", String.valueOf(adapter.getItemCount()));
});
Log.d("ItemCountOUTSIDE", String.valueOf(adapter.getItemCount()));
return rootView;
}
when I run the app the recycler view is empty, in the logs the "ItemCountOUTSIDE" shows 0 and is written earlier the "ItemCount" which shows 2. RoomDB is capable of showing the 2 items that are in it when the app is run, so i'd imagine its not a problem with the ViewModel. I was able to output the objects in the DB on a TextView using the same observer function and also using the ViewModel. I understand that apparently observer runs in separate thread and that is executed later but I dont know how to fix that since I know concurrency only on a surface level. Also when doing my research this pattern that I'm using seems to be working for others, hence my frustration.
Additional Code for the Adapter and ViewHolder:
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
public class Holder extends RecyclerView.ViewHolder {
private final TextView textView;
private Holder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textName);
}
public void bind(rEnt E){
textView.setText(E.rN);
}
static Holder create(ViewGroup parent){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv1_item, parent, false);
return new Holder(view);
}
}
and
import android.util.Log;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Query;
import java.util.List;
public class Adapter extends ListAdapter<rEnt, Holder> {
List<rEnt> myList = getCurrentList();
public Adapter(#NonNull DiffUtil.ItemCallback<rEnt> diffCB){
super(diffCB);
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return Holder.create(parent);
}
#Override
public void onBindViewHolder(#NonNull Holder holder, int position) {
rEnt currentEnt = getItem(position);
holder.bind(currentEnt);
}
static class Diff extends DiffUtil.ItemCallback<rEnt>{ //fuck is this as well???
#Override
public boolean areItemsTheSame(#NonNull rEnt oldItem, #NonNull rEnt newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull rEnt oldItem, #NonNull rEnt newItem) {
return oldItem.rN.equals(newItem.rN);
}
}
}
I am working on a chat app with firebase and I want to fitch the user information to the recyclerview...my code is correct by when I set the adapter to the recycler view its show an error.
Here is my code:
package com.abdelatif.chatapp;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
public class ChatFragment extends Fragment {
private FirebaseFirestore firebaseFirestore;
LinearLayoutManager linearLayoutManager;
private FirebaseAuth firebaseAuth;
ImageView mimageviewofuser;
FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder> chatAdapter = null;
RecyclerView mrecyclerview;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.chatfragment,container,false);
firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore= FirebaseFirestore.getInstance();
mrecyclerview=v.findViewById(R.id.recyclerView);
// Query query=firebaseFirestore.collection("Users");
Query query=firebaseFirestore.collection("Users").whereNotEqualTo("uid",firebaseAuth.getUid());
FirestoreRecyclerOptions<FireBaseModel> allusername=new FirestoreRecyclerOptions.Builder<FireBaseModel>().setQuery(query,FireBaseModel.class).build();
chatAdapter=new FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder>(allusername) {
#Override
protected void onBindViewHolder(#NonNull NoteViewHolder noteViewHolder, int i, #NonNull FireBaseModel firebasemodel) {
noteViewHolder.particularusername.setText(firebasemodel.getName());
String uri=firebasemodel.getImage();
Picasso.get().load(uri).into(mimageviewofuser);
if(firebasemodel.getStatus().equals("Online"))
{
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
noteViewHolder.statusofuser.setTextColor(Color.GREEN);
}
else
{
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
}
noteViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "contactClicked", Toast.LENGTH_SHORT).show();
}
});
}
#NonNull
#Override
public NoteViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_view_layout,parent,false);
return new NoteViewHolder(view);
}
};
mrecyclerview.setHasFixedSize(true);
linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
mrecyclerview.setLayoutManager(linearLayoutManager);
mrecyclerview.setAdapter(chatAdapter);
chatAdapter.startListening();
return v;
}
public class NoteViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder
{
private TextView particularusername;
private TextView statusofuser;
public NoteViewHolder(#NonNull View itemView) {
super(itemView);
particularusername=itemView.findViewById(R.id.nameOfUser);
statusofuser=itemView.findViewById(R.id.statusOfUser);
mimageviewofuser=itemView.findViewById(R.id.imageViewOfUser);
}
}
}
the error is in the line: mrecyclerview.setAdapter(chatAdapter);
and the error is:
'setAdapter(androidx.recyclerview.widget.RecyclerView.Adapter)' in 'androidx.recyclerview.widget.RecyclerView' cannot be applied to '(com.firebase.ui.firestore.FirestoreRecyclerAdapter<com.abdelatif.chatapp.FireBaseModel,com.abdelatif.chatapp.ChatFragment.NoteViewHolder>)'
I hope someone give me a solution...i searched many time but nothing.
Make sure you call chatAdapter.startListening(); before calling mrecyclerview.setAdapter(chatAdapter);
when i click on the "ChangeBudgetBtn" the next activity i.e. BudgetEdit does not open and the application closes without any prompt
package com.moneymgmt.moneymanagementsystem;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class BudgetFragment extends Fragment {
Button ChangeBudgetBtn;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_budget, container,false);
ChangeBudgetBtn = (Button) rootView.findViewById(R.id.ChangeBudgetBtn);
ChangeBudgetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),BudgetEdit.class));
}
});
return rootView;
}
}
Replace this line
final View rootView = inflater.inflate(R.layout.fragment_budget, false);
by the following line
final View rootView = inflater.inflate(R.layout.fragment_budget, container, false);
because you forgot to add the container parameter
I'm writing a simple program in which a two fragments and one activity is used. Both Fragments are displayed (one at a time) within the activity's Frame Layout. The first fragment is a listview that lists items from which the user can select, then the main activity should swap the first fragment with a detail fragment according to the item position determined by a listener within the first fragment. Trouble is, my program won't actually commence the swap. Here's the code for the activity:
package com.example.user.monkeys;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class ListActivity extends FragmentActivity implements
monkeyListFragment.OnMonkeySelectedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("Activity", "onCreate Pre-Fragment 1");
super.onCreate(savedInstanceState);
setContentView(R.layout.monkey_list_frame);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
monkeyListFragment monkeyList = new monkeyListFragment();
monkeyList.setArguments(getIntent().getExtras());
// Add the fragment to the container
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, monkeyList);
Log.i("Activity", "made it end onCreate");
}
}
#Override
public void onMonkeyItemSelected(int position) {
Log.i("From Activity", "onMonkeyItemSelected");
monkeyDetailsFragment newDetailFrag = new monkeyDetailsFragment();
Bundle args = new Bundle();
args.putInt("itemPosition", position);
newDetailFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newDetailFrag);
transaction.addToBackStack(null);
transaction.commit();
}
}
And the code for the listview fragment:
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyListFragment extends Fragment{
OnMonkeySelectedListener monkeyCallBack;
private ListView monkeyLV;
private String[] monkeyStrings;
public interface OnMonkeySelectedListener {
public void onMonkeyItemSelected(int position);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("Fragment 1", "Made it to onCreateView");
View view = inflater.inflate(R.layout.list_fragment, container, false);
monkeyLV = (ListView) view.findViewById(R.id.monkeyListView);
monkeyStrings = getResources().getStringArray(R.array.monkey_data_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, monkeyStrings);
monkeyLV.setAdapter(objAdapter);
AdapterView.OnItemClickListener monkeyListen = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ListFragment-ClckLstnr", "Made it");
monkeyCallBack.onMonkeyItemSelected(position);
}
};
Log.i("Fragment 1", "Made it past OnItemClick Listener");
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
monkeyCallBack = (OnMonkeySelectedListener) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
and finally the code for the detail fragment (this isn't fully fleshed out but it should still swap I believe).
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyDetailsFragment extends Fragment {
ImageView monkeyPicture;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
Log.i("From Detail Fragment", "Got here");
View view = inflater.inflate(R.layout.monkey_detail_fragment, container,
false);
return view;
}
}
I found your missing line of code for the listview fragment:
monkeyLV.setOnItemClickListener(monkeyListen)