How to find listview in 'snippet'

I have a snippet that sends a request to the server and loads json, and then parses it into a listview, since I cannot properly convey to the view. The following is part of my applications, this is my first touch on a listview with a fragment, for some reason I only allow the fragment to be used, so I select ' setAdapter ' instead of ' setListAdapter ' for 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 opinion, but it doesn’t work like that ... Now I just want to get my list and apply

MyListView.setAdapter(colorAdapter) to it

any help would be appreciated thanks

+4
source share
2 answers

Option 1: Since the fragment layout consists of a simple ListView. Consider using

Open Class Gallery Extends ListFragment

Then you can use this code to get the list.

myFragmentView.getListView ()

Option 2: As Nikolaus pointed out, you need a custom identifier if you want to use findViewById.

android: id = "@ + id / ListView 1"

+8
source

If you do not want to use ListFragment, you need to add your own identifier (android: id = "@ + id / custom_id"), then you can use findViewById to search for ListView

+2
source

All Articles