I have this code:
ListView listView;
static final String[] Liste = {"Satz des Pythagoras", "abc-Formel", "pq-Formel"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, Liste));
listView.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.maths:
return true;
case R.id.physics:
Intent myIntent2 = new Intent(Home.this, ActivityPhysics.class);
startActivity(myIntent2);
default:
return super.onOptionsItemSelected(item);
}
}
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
if ("Satz des Pythagoras".equals(Liste[position])) {
Intent sdp = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(sdp, 0);
}
else if ("abc-Formel".equals(Liste[position])) {
Intent abc = new Intent(view.getContext(), ActivityABC.class);
startActivityForResult(abc, 0);
}
else if ("pq-Formel".equals(Liste[position])) {
Intent pq = new Intent(view.getContext(), ActivityPQ.class);
startActivityForResult(pq, 0);
}
}
This should display a list and a actionbar.
It did, for some time.
It looks like I messed up the code and, since I’m not having that much experience, can’t find my mistake…so could someone review my code? 🙂
Because now it loads the activity..but doesn’t display the list or the actionbar 😮
Here is the XML in \menu\activity_home.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="@+id/maths"
android:title="@string/maths"
android:orderInCategory="1"
android:showAsAction="always|withText"
android:titleCondensed="@string/maths"
android:icon="@drawable/ic_action_calculator" />
<item android:id="@+id/physics"
android:icon="@drawable/ic_action_line_chart"
android:title="@string/physics"
android:orderInCategory="2"
android:showAsAction="always|withText"/>
This is layout\activity_home.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
</RelativeLayout>
So basically it’s empty.
With these two lines:
You are using the same layout for the entire Activity and each row, which doesn’t make sense. The Activity’s layout should contain a ListView and the row layout shouldn’t…
To use the default layouts, remove this line:
and change this one:
Also you need to
onListItemClick()notonItemClick()to have the row clicks work.You must give your TextView an id:
And change the way you create your adapter to: