android


Android how to open fragment from listview element


In MainActivity is FrameLayout MainContainer. I load there a fragment TrainerMyGroups, there is a Listview where I add a few elements (each element has some strings) by use TrainerGroupsAdapter. Actually I want to replace fragment TrainerMyGroups by another (for example TrainersInfo) by click on list's element.
My TrainerGroupsAdapter is:
public class TrainerGroupsAdapter extends ArrayAdapter {
List list = new ArrayList();
public TrainerGroupsAdapter(Context context, int resource) {
super(context, resource);
}
static class Datahandler{
TextView name;
TextView when;
TextView where;
LinearLayout ll;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row=convertView;
Datahandler handler;
SharedPreferences pref = getContext().getSharedPreferences("pref", Context.MODE_PRIVATE);
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row= inflater.inflate(R.layout.list_mygroups,parent,false);
handler = new Datahandler();
handler.name = (TextView) row.findViewById(R.id.trainermygroupslistname);
handler.where = (TextView) row.findViewById(R.id.trainermygroupslistwhere);
handler.when = (TextView) row.findViewById(R.id.trainermygroupslistwhen);
handler.ll=(LinearLayout) row.findViewById(R.id.trainermygroupslistlayout);
row.setTag(handler);
}
else {
handler = (Datahandler)row.getTag();
}
TrainerGroupsDataProvider dataProvider;
dataProvider = (TrainerGroupsDataProvider)this.getItem(position);
handler.name.setText(dataProvider.getName());
handler.when.setText(dataProvider.getWhen());
handler.where.setText(dataProvider.getWhere());
handler.ll.setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + dataProvider.getColor()));
handler.ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.MainContainer, new TrainerInfo()).addToBackStack(null).commit();
}
});
return row;
}
}
It doesn't work but method OnClick is probably good because if I replace getFragmentManager().beginTransaction().replace(R.id.MainContainer, new TrainerInfo()).addToBackStack(null).commit(); to code for change some strings (name, when tc) it works. Problem is in getFragmentMenager(), Android Studio shows message that I have to create Getter and AS's suggestion is to generate in OnClick method:
private FragmentManager fragmentManager;
public FragmentManager getFragmentManager() {
return fragmentManager;
}
then problem is in the second argument in replace, I have error that it has to be Fragment (Im sure that TrainersInfo() is fragment because I use it in other place and it works).
How can I solve this problem or what is the best way to open fragment by click on lise's element in another fragment?
#UPDATE2
Better then replacing the Fragment inside the apater is to say your activity that it should replace the fragment. This can be done with an interface which you implents inside your Activity:
public class TrainerGroupsAdapter extends ArrayAdapter {
// The interface which you have to implement in your activity
public interface OnChangeFragmentListener {
void changeFragment();
}
List list = new ArrayList();
private OnChangeFragmentListener m_onChangeFragmentListener;
public TrainerGroupsAdapter(Context context, int resource) {
super(context, resource);
m_onChangeFragmentListener = (OnChangeFragmentListener) context;
}
// Your other code
}
The OnClickListener in your getView Method:
handler.ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Call the method which change the fragment
m_onChangeFragmentListener.changeFragment();
}
});
The Activity:
public class MainActivity extends AppCompatActivity implements TrainerGroupsAdapter.OnChangeFragmentListener {
//...
// Your Other code
// Implement the method which is called in the adapter and replace the fragment here
#Override
public void changeFragment() {
getFragmentManager().beginTransaction().replace(R.id.MainContainer, new TrainerInfo()).addToBackStack(null).commit();
}
}
#UPDATE1
You need an activity context for getSupportFragmentManager() and getFragmentManager().
You can change the Context parameter of the constructor to Activity and create a member variable in your class for the activity so you can use it later:
public class TrainerGroupsAdapter extends ArrayAdapter {
List list = new ArrayList();
private Activity m_activity;
public TrainerGroupsAdapter(Activity context, int resource) {
super(context, resource);
m_activity = context;
}
// Your other code
}
The OnClickListener in your getView method:
handler.ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m_activity.getFragmentManager().beginTransaction().replace(R.id.MainContainer, new TrainerInfo()).addToBackStack(null).commit();
}
});

Related Links

Notification on Onfinish() function of a CountDownTimer class
Having trouble getting the correct search suggestions for Icelandic letters?
jQuery draggable() element doesn't start till after the second tap (on double tap only), any explanation?
Android bundle from pending intent always recieved as null
Dynamic Quick Settings tile in nougat
Android SIP Outoing call - how to enable audio on OnRingingBack event
How to continue with Rest calls if the first one satisfies the condition?
In Android, how to get default focus on Back/Home button on ActionBar
How to set bitmap to Unity's Texture on Android?
How to improve resolution of image on Android
How can we set a limit to the results came in #POST request in retrofit android
Get intent in Service return NullPointerException
How to get color of Toolbar's/ActionBar's text color programmatically?
Android Pay checkForPreAuthorization has been depcrecated, any alternatives?
How to debug a binder page fault
Mediarecorder.audiosource mic not record from headset mic

Categories

HOME
fabricjs
azure-stream-analytics
microsoftgraph
paraview
prism
dns
pascal
obfuscation
dojo
visual-studio-emulator
hugo
onclick
nuget-package
nfs
title
fabric.io
nancy
eip
magnetic-cards
docx
boost-icl
breadth-first-search
ksh
named-entity-recognition
interpreter
direct3d11
8051
datastax-enterprise-graph
complex-numbers
sql-server-2000
catch-unit-test
yowsup
processwire
git-rewrite-history
ideascript
amazon-ses
projects-and-solutions
wmp
xmldocument
oim
openresty
jquery-ui-draggable
serde
vugen
tf
aquamacs
stdmap
bbpress
reshape
swingworker
android-instrumentation
winusb
parfor
greatest-n-per-group
dup
vorpal.js
redis-cluster
highland.js
jnlua
sapi
xenomai
oracle-spatial
rate-limiting
zen-cart
android-launcher
telepat
aspectj-maven-plugin
kubuntu
zend-currency
fits
umlgraph
procedural-programming
at-job
browserify-shim
easyrtc
raygun
insight.database
ignite-ui
alternate
amslidemenu
getopt
windows-embedded-standard
gwt-openlayers
rolify
mvc-editor-templates
dolby-audio-api
traceability
argouml
tree-balancing
vfs
failing-tests
luabind
user-tracking
filemerge
cuteeditor
appledoc
twitterizer
photolibrary
message-passing
sifr
duplex
lxr
sqlobject
soappy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App