MainActivity.java
activity_main.xmlpackage net.macdidi.listviewadapter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.item_list); ArrayList<String> data = new ArrayList<>(); data.add("a"); data.add("b"); data.add("c"); int layoutId = android.R.layout.simple_list_item_1; ListView item_list = (ListView)findViewById(R.id.item_list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, layoutId, data); item_list.setAdapter(adapter); } }
<?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" 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="net.macdidi.listviewadapter.MainActivity"> <ListView android:id="@+id/item_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="1sp" android:divider="@color/divider_color" /> <TextView android:id="@+id/show_app_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Hello World" /> </RelativeLayout>
Correction:
Check are you using RelativeLayout, the most easy way is change that to LinearLayout and also add the line android:orientation="vertical" to LinearLayout.
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical" 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="net.macdidi.listviewadapter.MainActivity"> <ListView android:id="@+id/item_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="1sp" android:divider="@color/divider_color" /> <TextView android:id="@+id/show_app_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Hello World" /> </LinearLayout>
No comments :
Post a Comment