Can't set a Custom Listview on ListFragment - android

I'm trying to set a Custom Listview layout on a ListFragment, I've been reasearching everywhere and i still cant get it to work. I'm new programming with android, so I'll really appreciate your help.
This is my listFragment class
public class ListCins extends ListFragment {
public static ArrayList<Cin> cins;
private ListView listView;
private pListAdapter mAdapter;
public static ListCins newInstance() {
ListCins f = new ListCins();
return f;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Los fragments soportan presentar un msj cuando la lista esta vacia.
setEmptyText("Nothing yet, but working");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cins = new ArrayList<Cin>();
cins.add(new Cin("Agenter", 1));
cins.add(new Cin("Ora", 2));
cins.add(new Cin("Vistia", 2));
cins.add(new Cin("Bluel", 2));
mAdapter = new pListAdapter(this.getActivity(), cins);
setListAdapter(mAdapter);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setEmptyText("Nothing yet 2, but working");
registerForContextMenu(getListView());
setHasOptionsMenu(true);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast mTosat = Toast.makeText(this.getActivity(),"clicked:"+String.valueOf(position)+" "+cins.get(position).nombre , 200);
mTosat.show();
}
}
And this is the list_fragment.xml i want to apply to the listfragment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data" />
</LinearLayout>
I also have a custom arrayAdapter, but i dont know if it has something to do with the custom listview layout.
Thanks in advance...

You need to override onCreateView and inflate/return your custom layout. The ListFragment should handle the rest for you. This is described on the Screen Layout section of http://developer.android.com/reference/android/app/ListFragment.html

Thanks NasaGeek, I was trying to inflate it just like they said, like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
but it wasen't working, then i found the problem. For some reason this method makes the code goes like crazy.
setEmptyText("Nothing yet, but working");
If you could explain me why, it will be awesome.
Thanks.

Related

ListView Click in android fragment

I can't move from fragment to Activity on ListItem Click Listener i think i am doing it right but don't know where is problem.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
return inflater.inflate(R.layout.scholorship_listing_main,container,false);
}
#Override
public void onStart() {
super.onStart();
list=(ListView)getActivity().findViewById(R.id.Scholorshiplist);
setAdapterValue();
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), ScholorshipDetail.class);
getActivity().startActivity(intent);
//getActivity().finish();
}
});
}
And my Activity class on which I want to move is
public class ScholorshipDetail extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scholorship_detail);
}
}
and my xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.temp.scholorshipapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scholorships"
android:textColor="#color/black"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="80dp"
android:layout_centerHorizontal="true"
android:id="#+id/txt_scholorship"
android:divider="#C0C0C0"
android:dividerHeight="4px"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/black"
android:layout_below="#+id/txt_scholorship"/>
<ListView
android:layout_below="#id/txt_scholorship"
android:id="#+id/Scholorshiplist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#C0C0C0"
android:dividerHeight="4px"
/>
</RelativeLayout>
Post the code in onCreateview() instead of onStart() in your fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentlayout, container, false);
list=(ListView) view.findViewById(R.id.Scholorshiplist);
setAdapterValue();
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), ScholorshipDetail.class);
getActivity().startActivity(intent);
//getActivity().finish();
}
});
return view;
}
This might be your adapter layout contains the click listener view.i.e, button view or some other view, which will block the listview click listner. So in your adapter parent layout you need add block descendent or focusability as below,
android:descendantFocusability="blocksDescendants"
(OR)
android:focusable="false"
Hope this is helpful:)

What should i use to create custom ListView in Fragment to suit my need?

I've been creating News application. I used this tutorial to create good-looking Navigation Drawer. It uses fragments for every category in Navigation List. I will be using provided API to get news (in the JSON format), and i need customized List View to show the list of news. When you click list item, it will open new view to show details of news. I've found several tutorials, but none of them worked for fragment. Please, tell me way to do it.
I'm new to Android Programming. This is my first project, though it is too hard for being first project. But i have to do it. Please, help!
Thanks beforehand!
Assuming you know how to create an adapter and a custom row layout from a tutorial that works for activities. Minor modifications will help you using fragments.
Your fragment should look like this:
public class NewFragment extends Fragment{
CustomAdapter listAdapter;
ListView listView;
ArrayList<Data> yourData; // Should be filled with data from your JSONParser;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
/*
* Use your custom adapter. in this example the adapter need
* the context and an array that contains your data
*/
listAdapter = new CustomAdapter(getActivity(),yourData);
...
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_fragment, container, false);
listView = (LsitView)view.findViewById(R.id.your_list);
listView.setAdapter(listAdapter);
...
}
}
You can use for this ListFragment:
Here is a fully customized example:
activity_main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/frameLayout"/>
</RelativeLayout>
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
myitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="#+id/txt_mytext" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fr = mListFragment.newInstance();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.frameLayout, fr, "tag").commit();
}
}
mListFragment.java
public class mListFragment extends ListFragment {
public static mListFragment newInstance() {
mListFragment f = new mListFragment();
//Bundle args = new Bundle();
//f.setArguments(args);
return f;
}
public mListFragment() {}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] strings = {"a", "b", "c"};
ArrayAdapter<String> adapter = new LstAdapter(
getActivity(), R.layout.myitem,
strings);
setListAdapter(adapter);
return inflater.inflate(R.layout.mylist, null);
}
public class LstAdapter extends ArrayAdapter<String> {
private String[] mArray;
public LstAdapter(Context context, int textViewResourceId, String[] mList) {
super(context, textViewResourceId, mList);
mArray = mList;
}
#Override
public int getCount() {
return mArray.length;
}
#Override
public String getItem(int position) {
return mArray[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.myitem, null);
}
TextView tvName = (TextView) v.findViewById(R.id.txt_mytext);
tvName.setText(mArray[position]);
return v;
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("123", position + "");
super.onListItemClick(l, v, position, id);
}
}

ListView OnClickListener is not working within Fragment

I'm trying to put a listener to my listview, which is inside my fragment. I've tried a few ways to do it, but still not working. I cannot make the other view focusable="false" because there is an EditText above the listview. Everytime I click the item, it calls the getView in the JSONAdapter that I created. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<EditText
android:id="#+id/searchquery"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Search"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list">
</ListView>
</LinearLayout>
Here is my Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
mJSONAdapter = new JSONAdapter(getActivity(), inflater);
View v = inflater.inflate(R.layout.header, container, false);
return v;
}
#Override
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView lv = (ListView) getView().findViewById(android.R.id.list);
// Set the ListView to use the ArrayAdapter
lv.setAdapter(mJSONAdapter);//isi datany pk json adapter
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();
JSONObject jsonObject = (JSONObject) mJSONAdapter.getItem(position);//ambil item yg diclick
String raceid = jsonObject.optString("id","");//ambil cover id yg diklik, argumen kedua itu klo null defaultny apa
// create an Intent to take you over to a new DetailActivity
Intent detailIntent = new Intent(getActivity(), DetailActivity.class);
// pack away the data about the cover
// into your Intent before you head out
detailIntent.putExtra("raceid", raceid);
// start the next Activity using your prepared Intent
startActivity(detailIntent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
new RetrieveFeedTask().execute();
}
You can try this code.
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// do your work
}
});
It appears that I choose the wrong method. It should be OnItemClickListener(), instead I choose OnItemSelectedListener()

Passing data from ListView to Fragment

I have a ListView in my fist screen that shows some information. When I click on each item, I have another listView with new information and textview in the same page, I want to show the pervious list view row in text view,
would you please let me know how can I implement this,
Thanks in advance!
here is my xml file
<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"
tools:context="######">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone"
android:text="test" />
<TextView
android:id="#+id/list_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:text="#string/no_message"
android:textColor="#000000"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:gravity="center"></TextView>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
Here is my java file that shows the listview and
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView)view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getActivity().getActionBar().setTitle(R.string.title_forms);
TextView details = (TextView)view.findViewById(R.id.detail_header);
details.setVisibility(View.VISIBLE);
return view;
}
Here is one of my fragment that has onItemClick Listener
public class ListFragment extends Fragment implements AdapterView.OnItemClickListener {
public static final String TAG = "ListFragment";
private ListView listView;
private ArrayList<String> testIds;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
tripIds = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
registerForContextMenu(listView);
return view;
}
Here is my onItemClick :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
chooseTest(testIds.get(position));
String selectedTestId = testIds.get(position);
Helpers.saveToSharedPreferences(getActivity(),
Constants_Prefs.SELECTED_TOP_LEVEL_RECORD,
Constants_Keys.SELECTED_TEST_ID,
selectedTestId);
I don't know how can I get that information
FormTypeListFragment passDataTo = new FormTypeListFragment();
Bundle bundle = new Bundle();
bundle.putString("DATA", selectedTestId);
passDataTo.setArguments(bundle);
}
Before launching your fragment to display selected item use setArguments() method to add key value pair of string in your case, Then in your fragment on createView use fragments method getArguments() to retain your passed string
Pass the value in bundle as :
Set :
YourFragment fragmnt = new YourFragment ();
Bundle bundle = new Bundle();
bundle.putString("item","name");
fragmnt.setArgument(bundle);
Get :
Then in your Fragment, retrieve the data (e.g. in onCreate()) with:
Bundle bundle = this.getArguments();
String myValue = bundle.getInt("item", "");

ListView not showing items

I am trying to display a basic array of titles in a list view using fragments. And just not able to get the List items to show up. Wondering if anyone can shed some light. Also are there any good resources that help you understand Layouts well?
Here is my list_fragment.xml and the listfragment source file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#id/android:list"
android:drawSelectorOnTop="false"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
public class PublicTradeListFragment extends ListFragment
{
private String trades[];
public PublicTradeListFragment()
{
trades = new String [] {
"Trade ?",
"Give up ?",
"Trade again?"
};
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,trades);
setListAdapter(listAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.list_fragment,container,false);
}
#Override
public void onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
You can try this in "on Create View" then
"return super.onCreateView(inflater, container, savedInstanceState);"
Check this link: http://wptrafficanalyzer.in/blog/a-listfragment-application-in-android/

Categories

Resources