Hello, I am a noob developer for android, I am working with Custom List views, adapters,etc on activities and everything has been working good. Recently I am trying to incorporate these custom listviews to Fragments.
This is my code that worked with activitiesÑ
ADAPTER
`class ClientesListViewAdapter : BaseAdapter
{
List items;
Activity context;
public ClientesListViewAdapter(Activity context, List<Clientes> items) : base() { this.context = context; this.items = items; } #region implemented abstract members of BaseAdapter public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { var item = items[position]; View view = convertView; if (view == null) { view = context.LayoutInflater.Inflate(Resource.Layout.ClientesCustomView, null); } view.FindViewById<TextView>(Resource.Id.txtClienteNombre).Text = item.ClienteNombre; view.FindViewById<TextView>(Resource.Id.txtClienteCargo).Text = ("Cargo :") + item.ClienteCargo; view.FindViewById<TextView> (Resource.Id.txtClienteFaseActual).Text = ("Fase actual: ") + item.ClienteFaseActual; return view; } public override int Count { get { return items.Count; } } #endregion #region implemented abstract members of BaseAdapter public override Clientes this[int position] { get { return items[position]; } } #endregion }
}Activity:
public class ClientesActivity : Activity
{
List clientesThisList = new List ();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ClientesLayout);
// Create your application here
clientesThisList.Add(new Clientes(1,"Marta Gonzalez", "Presidenta","Supersayayin"));
clientesThisList.Add(new Clientes(2, "Marta ersdfg", "fgfhh", "fdhgj"));
ListView clientesListView = FindViewById<ListView>(Resource.Id.clientesListView); clientesListView.Adapter = new ClientesListViewAdapter(this, clientesThisList); // Create your application here } }`
And with the fragment I use the same adapter but in the fragment when i attempt to use this line it throws me a 4 errors:clientesListView.Adapter = new ClientesListViewAdapter(this, clientesThisList);
Can anyone please help me to adapt my activity code into a Fragment or ListFragment functional code? I have spent 3 days without any luck.
Your help will be appreciate.
This is my "ListFragment" code so far:
`public class ClientesFragment : ListFragment
{
List clientesThisList = new List ();
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// A large amount of text to display. //var view = inflater.Inflate(Resource.Layout.ClientesLayout, container, null); var view = inflater.Inflate(Resource.Layout.ClientesLayout, null);
// clientesThisList.Add(new Clientes(1,"Marta Gonzalez", "Presidenta","Supersayayin"));
// clientesThisList.Add(new Clientes(2, "Marta ersdfg", "fgfhh", "fdhgj"));
ListView listview;
listview = view.FindViewById (Resource.Id.clientesListView);
ListView.Adapter = new ClientesListViewAdapter (this, ClientesListViewAdapter);
//listview.Adapter = new ClientesListViewAdapter (this, clientesThisList);
return view;
}
}`
Posts
If you are implementing a custom ListView in a Fragment do not use ListAdapter.
Thanks for your reply Cheesebaron, can you elaborate a little more?
I´m little lost here, what should I use instead of ListAdapter?
Thanks in advanced!
Sorry I meant to write ListFragment. Don't use a ListFragment when using a custom ListView, use a normal Fragment.
Thank you but I can't make it work, this is my fragment code to call the activity that holds the custom view:
`
public class ClientesFragment : Fragment
{
List clientesThisList = new List ();
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
`
In this case it shows an empty screen, but if i change the layout resource in the inflater to my raw custom view:
View view = inflater.Inflate(Resource.Layout.ClientesCustomView, null);
I get only the first element of my list.
How can I show the whole list view?
I really appreciate your help!
Please format your code or make a reproducible sample on GitHub or similar.
If you paste the code here, mark it and press the C button in the toolbar it will be a lot easier to read it.
ADAPTER:
ACTIVITY
FRAGMENT:
With this line changed it shows only the first item of the list:
View view = inflater.Inflate(Resource.Layout.ClientesCustomView, null);
I don't see anywhere where you are actually using your Fragment.
I forgot this: I used a tabbed layout using action bar
Can you show how you define
clientesListView
?Are you sure you want the height of the
ListView
to bewrap_content
?Well I have changed ti so many times, I don´t think that it´s the problem, right now I'm testing with fill_parent and it doesn't show anything....
Not really sure what you are doing, but it kind of seems strange to that you both have an Activity and a Fragment using the
clientesListView
. Where are you changing to theClientesActivity
? Where are you populating theClientesFragment
with anAdapter
?if i understand true you can use adapters in fragments like that..
// replace this
I wanted to use the Action Bar tab layout and what I read is that it works with fragments, so I inflate my already made layout from a fragment and I populate the list views from the activity:
So basically I only use the fragment as a medium to use the activity with action bar tabs
That won't work. You have to populate the list inside the Fragment if that is what you are showing. You can't just randomly make an Activity, not use it and expect that the ListView inside the Fragment gets populated...
But when I try to populate it directly in the Fragment, my code doesn't work, do you have a guide on How to populate a Fragment using a custom list view? I'm trying to populate my fragment with this code:
but it says that it can't convert a Fragment to Activity, so how can I make this work??
Instead of
this
usethis.Activity
because you are in aFragment
.if you use Activity in Adapter you must use in Fragment this.Activity please try this:
client.Adapter = new ClientesListViewAdapter(this.Activity, clientesThisList);
Seems like the question-maker here never came back. I just want to say thanks a lot for the answers here. They saved me when i got stuck. Havent worked with fragments before, and it confuses me.
sorry,
the this.Activity worked for me. I don't know how to mark this question as answered.
Thanks for the help, everyone!!!
@wonderWoman greatttttt ... it's work for me , thanks
Awesome... This works for me... Thanks @Cheesebaron and @wondorWom
Hey guys,
I have generated code from above code samples But i still getting error as unhandled exception
Following is my code
ClientesFragment :
`public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
If i set view layout as
ClientesLayout
getting unhandled exception while returning view.If i set view layout as
ClientesCustomView
getting null listview; (I know ClientesCustomView doesn't contain any listview ).I don't know how it's worked for @ActivamosDev