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

SQLiteDatabase warning Android
Android:How to apply theme to android application
binding to running Service (after finish() ) / callback Handler
Android, ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”!
How to route audio through bluetooth headphones on Android 4.0.3 rooted tablet
Run full screen on HTC Touch HD2 with Android 2.3
how to rotate the android widget text? and something I don't understand
Change background color on Android Listview Item in non touch mode
WebSockets for Android in Phonegap application with Node.js/Socket.IO server&client
Android Service error to ui
Errors playing YouTube content from within an Android Webview on Google TV
Is there any open sourced android voice recognition application available? [closed]
Is there any way to play .flv viles on VideoView or anything surface?
why webview not perform any action onclick of video in it?
Creating a main game thread and getting game to run
Browse DropBox in Android SDK

Categories

HOME
httparty
search
hyperlink
design-patterns
wakanda
redux-form
formal-languages
jax-rs
interface
can
static-site
carousel
parameters
big-o
onclick
iis-6
nfs
recordrtc
google-chrome-app
ibeacon-android
alamofire
redis-sentinel
filechannel
glyphicons
template-engine
sigma.js
gmm
imagemap
exploit
resourcebundle
perlbrew
jackson-modules
delimiter
alm
kudu
hibernate-validator
dsx-desktop
openwhisk
dynamic-jasper
html-encode
jwplayer6
numpy-broadcasting
servicestack-text
sim-toolkit
ods
typesafe-config
unordered-map
postgresql-8.4
movilizer
smoothstate.js
tcc
rfc3339
ovf
lampp
robomongo
wcftestclient
dtsearch
geotiff
twilio-click-to-call
context-sensitive-grammar
oozie-coordinator
flyout
chromium-os
node-request
angular2-testing
meteor-packages
reporting-services-2012
envi
rspec2
cifilter
cycle2
design-principles
highland.js
qt5.2
morton-number
google-books
coda
eoferror
zscript
rdflib
dnssec
fail2ban
spy++
teaspoon
codeplex
cancellation
gae-quotas
ecslidingviewcontroller
mt4j
gtm-oauth2
ie8-compatibility-mode
windows-phone-8-sdk
flex++
epiceditor
onload-event
mysql-error-2013
pyunit
correlated-subquery
fusefabric
virtualpathprovider
urlrewriter
jquery-tools
catransform3d
lr
multiplatform
ruby-1.8
isapi-extension
build-environment
contentpresenter
wcf-proxy
leader
idatareader

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