I am new to android and have been following udacity's video lectures.
The list view is not getting populated by the array adapter and when
i ran the app on mobile the app showed only the main activity layout.
the list view is not appearing on the mobile screen ,can anyone tell me whats the error here,is it something related to frame layout or is there an
error in
array adapter.
Thanks in advance
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceHolderFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public PlaceHolderFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray={
"Today","Tommorow","wed","Thursday","Friday","Saturday","Sunday"
};//creating some fake data to show in list view
List<String> fore=new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter=new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView)rootview.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
return rootview;
}
}
}
activity_main_xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="#+id/container"
tools:context="com.saxenarc.jeetesh.layouts.MainActivity">
</FrameLayout>
list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/list_item_forecast_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
>
</TextView>
fragment_main.xml
<?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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast"></ListView>
</FrameLayout>
Checked everything thrice before posting it.
Your code and list is working well but you haven't told the list where to be displayed. You can fix this in 2 ways.
First
Add the fragment inside the main activity since its not visible.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager.beginTransaction().replace(R.id.container, new PlaceHolderFragment).commit();
}
Second
Stop using the fragment and use the listview inside the Main activity
activity_main.xml
<?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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast"></ListView>
</FrameLayout>
MainActivity.java
List<String> fore = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(this,R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView) findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
Related
I have made a ListView on a Fragment.
public class MyList extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.mylist, container, false);
return view;
}
}
This is the xml file where is the ListView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ddffff"
xmlns:app="http://schemas.android.com/apk/res-auto"
>>
<android.support.wearable.view.WearableListView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"></android.support.wearable.view.WearableListView>
</android.support.wearable.view.BoxInsetLayout>
When I run my app the listview does not appear. The entire screen is black. What I want to know is why this is happening.
Because of the typo in the following line:
xmlns:app="http://schemas.android.com/apk/res-auto"
>>
It cannot read >>. Android doesn't throw an exception for this typo
I have a Fragment in my MainActivity, that displays a Digital Clock. It works: It shows the current system time. But it looks rather simple. So before I start trying to style this clock I wanted to ask, if there is a way, to use the systems Digital-Clock Widget?
Screenshot of the widget, that I'd like to use in my Fragment:
FragmentClock.java:
public class FragmentClock extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_clock, container, false);
return view;
}
}
fragment_clock.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutClockDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">
<DigitalClock
android:id="#+id/digital_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>
I'm trying to follow the Udacity Developing Android Apps course. I got to Lesson 3.03. Here, we are instructed to launch a new Activity (DetailActivity) from ForecastFragment when clicking an item in a list.
However, I keep getting an error when attempting to launch the DetailActivity. Binary XML file line #1: Error inflating class fragment, where it's caused by DetailActivity.java in it's onCreate() method.
I've already searched around, but none of the fixes suggested for this issue helped me at all.
The specific error is:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class
com.myapp.sunshine.app.DetailActivityFragment that is not a Fragment
DetailActivity.java
public class DetailActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}
DetailActivityFragment.java
public class DetailActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}
}
activity_detail.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.myapp.sunshine.app.DetailActivityFragment"
tools:layout="#layout/fragment_detail" android:layout_width="match_parent"
android:layout_height="match_parent" />
I should mention that I'm not using support libraries--as I'm targeting SDK 21+, Lollipop only.
This isn't the only fragment we've created in the course either. The other fragment we created looks identical to this one.
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.myapp.sunshine.app.ForecastFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
And for good measure, I'm including the ForecastFragment class. I've omitted unnecessary parts of code.
ForecastFragment.java
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> mForecaseAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 74/58",
"Sunday - Rainy - 65/59",
"Monday - Cloudy - 72/67"
};
final List<String> weekForecast = new ArrayList<>(Arrays.asList(forecastArray));
mForecaseAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecaseAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String forecast = mForecaseAdapter.getItem(position);
Intent detailActivityIntent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(detailActivityIntent);
}
});
return rootView;
}
}
fragment_detail.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=".DetailActivityFragment">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
which is nearly identical to
fragment_main.xml
<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" 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=".MainActivityFragment">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
You should be using FragmentActivity instead of Activity
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}
I could manage to create a TabAdapter with pager (swiping tabs), which implements a ListFragment for each. Then I called my items through the strings. This works just fine.
But the ListView here implements plain text.
My question: how can implement a custom ListView with custom Rows (Text and image)?
Here is my code:
.MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pager = new ViewPager(this);
pager.setId(R.id.pager);
setContentView(pager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, pager);
mTabsAdapter.addTab(bar.newTab().setText("Grundlagen"), ListViewTabGrundlagen.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Anästhesie"), ListViewTabAnaesthesie.class, null);
}
ListViewTabGrundlagen
public class ListViewTabGrundlagen extends ListFragment {
String[] list_items;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.list, container, false);
list_items = getResources().getStringArray(R.array.listGrundlagen);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list_items));
return rootView;
}
}
list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pager" />
</RelativeLayout>
Any workaround?
Thank you very much!
You need to look for the custom adapters, You can start by extending the BaseAdaper or ArrayAdapter and Overriding the proper methods in the classes.
I'm trying to get a frame layout in my app. I have two tabs, each tab is a fragment.
In one tab (listview) I want that two Seekbars appear when a button on the action bar is pressed.I know that I have to use Framelayout but not how to apply it? Please give me some hints how to get this done
Thanks
I think may be you can reference my sample follow ,it use android.support.v4 library.
At first, write a layout file for your activity like this :
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.app.FragmentTabHost
android:id="#+id/frag_tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
then in your MainActivity code, you can do like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
initializeTabHost();
}
/**
* 初始化TabHost
*/
private void initializeTabHost() {
tabHost.setup(this, getSupportFragmentManager(), R.id.content_layout);
TabSpec tabSound = tabHost.newTabSpec(TAB_SOUND).setIndicator(
getString(R.string.settings_sound));
TabSpec tabAdvanced = tabHost.newTabSpec(TAB_ADVANCED).setIndicator(
getString(R.string.settings_advanced));
TabSpec tabLocal = tabHost.newTabSpec(TAB_LOCAL).setIndicator(
getString(R.string.settings_local));
tabHost.addTab(tabSound, SoundFragment.class, null);
tabHost.addTab(tabAdvanced, AdvancedFragment.class, null);
tabHost.addTab(tabLocal, LocalFragment.class, null);
}
and make this class extends FragmentActivity.
then write a class extends Fragment, override the onCreateView method :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
return view;
}
Hope it can help you.