Ich bin durch die Android-Dev-Ausbildung bei Udacity.com, gefolgt zusammen mit der Umsetzung der Sonnenschein app.Ich benutze Android Studio neueste Version für die Umsetzung.Udacity Sunshine App-Lektion 1
Ich bin an dem Punkt zu bekommen, wo ich die Mock Listview bekommen soll und ich bekomme nichts auf dem Bildschirm und es zeigte keine Fehler.
Hier ist mein mainActivity.java Code
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] foreastarray={
"SUN-CLOUDY","MON-SUNNY","TUE-FOGGY","WED-CLOUDY","THU_ASTEROIDS","FRI-HEAVYRAIN","SAT-SUNNY"
};
List<String> weakforecast=new ArrayList<String>(Arrays.asList(foreastarray));
ArrayAdapter<String> mForecastAdapter=new ArrayAdapter<String>(getActivity(),
R.layout.list_item_forecast,weakforecast);
ListView listView=(ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}
}
Hier Code meine content_main.xml ist
<fragment 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:id="@+id/fragment"
android:name="com.example.android.sunshine.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
Hier mein fragment_main.xml Code hier ist
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.sunshine.MainActivityFragment"
tools:showIn="@layout/activity_main">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast">
</ListView>
</FrameLayout>
Schließlich ist mein list_item_forecast.xml Code
Bitte helfen Sie mir
Schauen Sie sich meinen Vorschlag an, verwenden Sie keine Fragmente an dieser Stelle. Verwenden Sie eine einfache leere Aktivität und fügen Sie den Code in die OnCreate-Methode der Aktivität onCreateView des Fragments ein. Das Arbeiten mit Fragmenten und die Verwaltung ihres Lebenszyklus könnten Kopfschmerzen bereiten. –