Java Code:
public class BlankFragment extends Fragment {
public BlankFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_blank, container, false);
ListView listView=(ListView) view.findViewById(R.id.listview);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("example");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
}
XML code:
<FrameLayout 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="layout.BlankFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="204dp"
android:id="#+id/listview"
android:background="#d36868"
android:layout_gravity="center"></ListView>
</FrameLayout>
This is my code ... I have initialize a ListView and i tried to add an item called "example" . When i run this app I can see my listview but without any ietm.
There is no problem with the code above.Because I tried.
I think it's some error code that you didn't get the code out of it.Such as Other Fragment,Activity,and so on.
Try to use wrap_content instead of fixed height
<FrameLayout 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="layout.BlankFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listview"
android:background="#d36868"
android:layout_gravity="center"></ListView>
</FrameLayout>
Related
I am working on a school project, in which i have a main_activity with two fragments. One of these fragments contains a listview. The listview gets filled through an adapter, and this all seems to work fine. However i try to add an OnItemClickListener to this listview, and i really can't get it to fire.
This is the fragment with the listview (overview_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
This where I add the listener in the overviewFragment.java:
public class OverviewFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_fragment, container, false);
final ListView listView = view.findViewById(R.id.list_view);
AdapterView.OnItemClickListener mMessageClickedHandler = new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long
id) {
Log.d(OverviewFragment.class.getSimpleName(), "listener in overviewFragment");
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
return view;
}
Here is the list item i use:
<?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"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/name"
/>
You should add items and adapter for the on click listener to work. Remove android:descendantFocusability="blocksDescendants", blocksDescendants removes focus for all the children in the ViewGroup.
I have the activity_main.xml with one FrameLayout relative to a ListViewFragment. I want to show this fragment in this activity. But its not working the ListView is not appearing. Do you know where is the issue?
main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_list"
/>
</android.support.constraint.ConstraintLayout>
Main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here Im trying to add the fragment A to the activity but the listview is not appearing
getSupportFragmentManager().
beginTransaction().add(R.id.fragment_list,new ListViewFragment()).commit();
}
}
listview xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
ListViewFragment:
public class ListViewFragment extends Fragment {
private ListView editor=null;
ArrayList<String> items = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_list, container, false);
editor=(ListView) (result.findViewById(R.id.listView));
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items);
editor.setAdapter(arrayAdapter);
return result;
}
}
Add height and width to the framelayout.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_list" />
Also your listView Fragment cannot have listview as parent layout.So wrap it with a layout like frame layout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
having a trouble here with the sliding tabs. So I have completed the tutorial but when i had a ListView to my Pager it only shows on the bottom of the screen.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.ambidata.innovway.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white"/>
</LinearLayout>
And my XML from ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/issue_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"/>
</LinearLayout>
Fragment Code:
public class TabIssues extends Fragment {
private ListView mainListView ;
private CustomListAdapter_Issues listAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle b = getArguments();
int user_id = b.getInt("user_id");
boolean all = b.getBoolean("all");
WSConnectionIssues runner = new WSConnectionIssues(user_id, all);
runner.execute();
while (runner.download == AsyncTask.Status.RUNNING)
{
try
{
Thread.sleep(1);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
View view = inflater.inflate(R.layout.activity_issues, container, false);
// Find the ListView resource.
mainListView = (ListView) view.findViewById(R.id.issue_list);
registerForContextMenu(mainListView);
// Create ArrayAdapter using the planet list.
View convertView = inflater.inflate(R.layout.row_issues, null);
listAdapter = new CustomListAdapter_Issues(this, runner.listIssues, convertView);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
return view;
}
Im using the phone for debug...as you can see blank...then the first item of my listview and if you scroll the others are there!
Could you try the listview layout as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/issue_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"/>
</RelativeLayout>
I am using the masterdetailsflow template to build a custom detail layout. it doesn't seem to work well. The closest question asked before does not seem to be solving my problem at
ListView not showing up in fragment
I put a Log to do debug, it seems to pass the "Check1" but it does not displaying.
public class RouteItemDetailFragment extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_routeitem_detail, container, false);
if (mItem != null) {
((TextView) rootView.findViewById(R.id.routeitem_detail)).setText(mItem.title);
Log.i("GPS", "onCreateView - RouteItemDetailFragment - check listadapter");
}
return rootView;
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
ArrayList<String> listItems = new ArrayList<String>();
listItems.add("helo1");
listItems.add("helo2");
listItems.add("helo3");
listItems.add("helo4");
ArrayAdapter<String> adapter;
((TextView) view.findViewById(R.id.txt1)).setText("kkkkkk");
Log.i("Check1", "onCreateView - RouteItemDetailFragment - check listadapter");
adapter=new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
listItems);
adapter.notifyDataSetChanged();
ListView listAdapter = (ListView) view.findViewById(R.id.lv);
listAdapter.setAdapter(adapter);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/twopanecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="?android:attr/textAppearanceLarge"
android:id="#+id/routeitem_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".RouteItemDetailFragment" />
<TextView
android:id="#+id/txt1"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
/>
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Your LinearLayout displays Views horizontally by default, so your second TextView and ListView are probably just being displayed off screen. Change the orientation to "vertical":
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/twopanecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
I am new to Android. Actually I want to create three listviews with the help of fragments. Is it possible? if possible then please give some reference. Any help in this regard will be well appreciated.
You mean you want make a fragment with 3 listview?
Just write a YourFragment extends Fragment, which with a layout contain 3 listviews.
I think ListFragment is useless in this situation, it just like ListActivity.
Well, if you want three ListFragments, it goes something like this.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp">
<fragment class="com.fragtest.Fragment1"
android:id="#+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment class="com.fragtest.Fragment2"
android:id="#+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment class="com.fragtest.Fragment3"
android:id="#+id/fragment3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
And then an xml for each fragment.
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
And then finally some code to hang it all together. Your main activity:
public class ListFragmentExampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And a class for each fragment to populate the lists... Fragment1.java:
public class JobFragment extends ListFragment {
private ScheduleDBAdapter mDBHelper;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDBHelper = new ScheduleDBAdapter(getActivity());
mDBHelper.open();
fillData();
}
private void fillData() {
Cursor jobsCursor = mDBHelper.fetchAllJobs();
getActivity().startManagingCursor(jobsCursor);
String[] from = new String[] { ScheduleDBAdapter.JOB_NUMBER,
ScheduleDBAdapter.JOB_PART };
int[] to = new int[] { R.id.ListItem1, R.id.ListItem2 };
SimpleCursorAdapter jobs = new SimpleCursorAdapter(getActivity(),
R.layout.listlayoutdouble, jobsCursor, from, to);
setListAdapter(jobs);
}
}
Hope that helps!