Fragment Class with ListView? - android

Is it possible to have custom ListView inside Fragment class?
This is my fragment class wherein the fragment_find_people is just an empty XML:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
return rootView;
}
}
Now, I've already have a customListView but it only works with the Activity class. How can I transfer that to Fragment?
my listview_main xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listmain" >
<ListView
android:id="#+id/listview"
android:layout_marginTop="10dp"
android:background="#drawable/bg"
android:divider="#9c9c9c"
android:dividerHeight="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></RelativeLayout>

Class context replace with getActivity thats only difference and replace view not activity:
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
//now you must initialize your list view
Listview listview =(Listview)rootView .findViewById(R.id.your_listview);
//EDITED Code
String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
//To have custom list view use this : you must define CustomeAdapter class
//listview.setadapter(new CustomeAdapter(getActivity()));
//getActivty is used instead of Context
return view;

Inside the onCreateView method first define your layout.
RelativeLayout rl=(RelativeLayout)inflater.inflate(R.layout.status,container, false);
list=(ListView) rl.findViewById(R.id.list1);
itemList = new ArrayList<HashMap<String, String>>();
// fill the defined arraylist using and asynctask or any other method
adapter=new Customadapt(context.getActivity(), itemList);
list.setAdapter(adapter);
return rl;

you can create your custom list view programmatically also. for ex:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return new CustomListView(container.getContext());
}

Inside onCreateView(...) method, call your CustomListAdapter like this
adapter = new CustomListAdapter(getActivity());
customListView.setAdapter(adapter);
And your CustomListAdapter as follows , e.g
private class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater;
public Context context;
public CustomListAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(this.context);
}
.......
.......
}

First off, yes, it is possible. Here is how.
As with all other resources, there are two ways to declare them. Programatically and via XML layout files. I'll explain using XML as it is what I use most often and I find best.
In your fragment_find_people XML, declare your layout as normal. In that layout, I presume at least a LinearLayout, you need to add a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_find_people_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#id/your_list_view_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Now you can just instantiate that ListView using:
Listview listView =(ListView)rootView.findViewById(R.id.your_list_view_id);
In order to use the "same" ListView, I would recommend simply duplicating the code of the ListView from one layout file, the one for your Activity to the Fragment. You could also separate the declaration of the ListView into a separate XML, which would be tidier if you'd be to reuse it a lot, but for a small project, just copy it.

Related

How to add a button at the end of a ListView in a Fragment?

I have a fragment activity which contains a ListView, and i need to add a button at the bottom of the list.
This is a part of the code:
public class listview1 extends Fragment implements OnItemClickListener {
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstances){
View rootView = inflater.inflate(R.layout.activity_main, container, false);
list = (ListView) rootView.findViewById(R.id.listView);
list.setAdapter(new CustomBaseAdapter(this.getActivity()));
list.setOnItemClickListener(this);
return rootView;
}
- CustomBaseAdapter extends a BaseAdapter which has a getView() method and all...
just go to your xml file and type this code inside the
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
I personally tested it and found it working
The xml file is in res -> layout ->yourfilename.xml

ParseQueryAdapter for ListFragment

As ParseQueryAdapter is meant to be used with an Activity - what is the general design idea for using a ParseQueryAdapter for a ListFragment? I want to display a custom list view.
I've tried to use a ParseQueryAdapter with a ListFragment, but the images are not displayed properly. If I use the exact code with an Activity, the images load correctly.
How can you use a ParseQueryAdapterin conjunction with a ListFragment?
I've posted the general code, but the text from the query loads wonderfully in the list, the images however don't always load or when they do..take a LONG time to show up.
<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" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:divider="#null"
android:dividerHeight="0px"
/>
</RelativeLayout>
public class PlacesTab extends ListFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.places_tab, container, false);
mainAdapter = new ParseQueryAdapter<ParseObject>(getActivity(), “Places”);
mainAdapter.setTextKey("name");
mainAdapter.setImageKey(“placeImage");
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
return rootView;
}
}//end class

ListView not appearing in fragment

So I have two Fragments inside an Activity using a PageAdapter.
One of the Fragments is a Listview and the other is a GridView of images.
The fragments are created successfully but the ListView is empty
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You should not check null for listview. But rather check if the list is empty.
Change it to listView.getChildCount()==0.
But I think you dont need to check because it will always be empty because it's a new created view. And no items on the list. If you want to check correctly, put the condition check after you set the adapter to the list.
Check if this works.

How to find listview in 'fragment'

I am having a fragment that send request to server and download json , then parse it into a listview , somehow i can't reach the view properly.below is some part of my apps , this is my first touch on listview with fragment , for some reason i only allow to use Fragment so i choose 'setAdapter' instead of 'setListAdapter' to be my adapter
myfragment.java
public class gallery extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.tab_frag2_layout,
container, false);
return myFragmentView;
}
public void onActivityCreated(Bundle savedInstanceState) {
.........
.........
.........
setAdapter(colorAdapter);
}
layout.tab_frag2_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding"
android:tag="listf" />
</LinearLayout>
My expectation is using
MyListView = (ListView)myFragmentView.findViewById(R.id.list);
to get my view , but it doesn't work that way...
Now I just want to get my listview and apply
MyListView.setAdapter(colorAdapter) to it
any help would be appreciated, thanks
Option1: Since your fragment layout consists of just a simple ListView.
Consider using
public class gallery extends ListFragment
Then you can use this code to get your listview.
myFragmentView.getListView()
Option2: As Nickolaus mentioned, you need a custom id if you want to use findViewById.
android:id="#+id/listview1"
If you don't want to use a ListFragment, you need to add a custom id (android:id="#+id/custom_id") then you can use findViewById to find the ListView

Android, issues with custom listview

I'm using a custom listview I found here:
http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_9.html
Seems to be valid in Eclipse, and looks good in the preview tab. It's just a listview that has a button on the bottom. So i've added it as R.layout.buttonlist
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/testbutton"
android:text="#string/hello" android:layout_alignParentBottom="true" />
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/list"
android:layout_alignParentTop="true" android:layout_above="#id/testbutton" />
</RelativeLayout>
Unfortunately when I run it, i get a pop up window that says Android has closed unexpecitdly:
setListAdapter(new ArrayAdapter<String>(this, R.layout.buttonlist , data));
When I try using a built in list view:
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
everything works fine. I dont see any errors or warnings in logcat, so I'm not sure how to pinpoint the problem. Does anyone have any ideas? Thanks
Edit: adding activity
public class TestActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> data = new ArrayList<String>();
data.add("hello");
data.add("world");
setContentView(R.layout.buttonlist);
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
}
}
Hoofamon, I would like to correct you here. You are not creating a custom ListView but a custom layout with a ListView. Also, I believe that you have not completely understood what the setListAdapter is doing here.
This line that you have is telling the listview to consume 'android.R.layout.simple_list_item_1' as the content of its layout. This layout comes pre-defined in the Android SDK. It would just contain text in each item of a listview. The third attribute 'data' indicates the content of each listview item.
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
So, as Mike L. has suggested, if your intent is to have a listview with only text (having the default format), then the line above would serve the purpose well. You can set 'R.layout.buttonlist' as the layout of your activity using
setContentView(R.layout.buttonlist);
However, if you are planning to include additional content in the listview (read images) or want to change the styling of the text, you would have to define a custom layout for the listview. We can direct you to appropriate sources if you want to know how that can be done.
EDIT: A possible way of loading data into a normal ListView
TestActivity.java
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonlist);
List<String> data = new ArrayList<String>();
data.add("hello");
data.add("world");
ListView mListView = (ListView)findViewById(R.id.list);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
}
}
buttonlist.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/testbutton"
android:text="#string/hello"
android:layout_alignParentBottom="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_above="#+id/testbutton" />
</RelativeLayout>
This is how it should look like on the emulator:
I don't think you can have a listview in the layout of an adapter. The passed in layout should just describe a row in the listview. So buttonlist should just contain the xml for the button. The listview needs to be in a separate layout file. If this is a list activity then you don't need another layout file, just call setListAdapter like you are doing.
If you want to use your R.layout.buttonlist to fill up your listview,you can do it as follows(your TestActivity should extend Activity,not ListActivity):
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonlist);
String data[]=new String[]{"Item_1","Item_2","Item_3"}
Button b=(Button)findViewById(R.id.testbutton);
ListView lv=(ListView)findViewById(R.id.list);
ArrayAdapter aa=new ArrayAdapter(context,android.R.layout.simple_list_item_1, data);
lv.setAdapter(aa);
//Your code...
}
Now if you want to create custom listitem to be displayed in the listview,then you need to do like this:
Create your custom listitem xml file.
Ex: custom_listitem.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/text"
/>
Create custom ArrayAdapter:
Ex. CustomArrayAdapter.class
public class CustomArrayAdapter extends ArrayAdapter<String> {
String[] array;
LayoutInflater mInflater;
public CustomArrayAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
array=objects;
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.custom_listitem, null);
holder = new ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.img=(ImageView)convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.text.setText(array[position]);
if(position==0)
holder.img.setImageResource(R.drawable.img1);
else if(position==1)
holder.img.setImageResource(R.drawable.img2);
else if(position==2)
holder.img.setImageResource(R.drawable.img3);
return convertView;
}
static class ViewHolder
{
TextView text;
ImageView img;
}
}
Use this custom adapter class in your main activity to fill up listview:
Be sure,this main activity extends Activity and not ListActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=getApplicationContext();
lv=(ListView)findViewById(R.id.listview);
CustomArrayAdapter aa=new CustomArrayAdapter(context,R.layout.custom_listitem, new String[]{"item_1","item_2","item_3"});
lv.setAdapter(aa);
// other lines of code
.
.
.

Categories

Resources