When I try to set the listAdapter or the adapter( #setListAdapter(), #getListView().setAdapter()
) it does not show. When I tried to set a ListView individually it works just fine. It doesn't work either when I run it in #runOnUiThread()
. Why is my list not showing?
The following is my code
public class ShopActivity extends ListActivity {
private Intent mainIntent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
mainIntent = new Intent(this, MainActivity.class);
String[] items = {"Test"};
ArrayAdapter adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
//ListView view = (ListView) findViewById(R.id.list);
//view.setAdapter(adapter);
getListView().setAdapter(adapter);
}
public void onBack(View view) {
startActivity(mainIntent);
}
}
EDIT:
layout:
<?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="wrap_content"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="450dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="@string/button_back"
android:onClick="onBack"
android:id="@+id/back"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMPTY"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="-500dp"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.11"/>
</LinearLayout>
set your linearlayout height to match_parent, and get rid of the weight as you its used for ratios and doesnt look like you are using it properly. Adding -500 margin is not the right way of doing it. Replace your linearlayout start with the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
If you want your TextView be at the top add it before the button, then add margin to the button to get a margin the text view. If you want your back button on the side next to the textview, add them to a linearlayout with horizontal orientation and which will add them at the top side by side.
You may want to use
this.setAdapter(adapter);
instead.
Make sure the layout R.layout.shop
has an ListView
element with id @android:id/list
. See ListActivity .
If you are using a ListActivity, in your xml, the listview should look like this with android:id="@android:id/list"
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
The problem was that for some reason the LinearLayout was located to low on the screen so I could not see it. I add android:layout_marginTop="-500dp"
to the ListView and it showed correctly :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.