I am trying to use the ListView
Component and I am stuck.
I'm only trying to view a list in my activity. I searched the internet for a solution,
but I don't know what is wrong in my code
activity_main.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:id="@+id/activity_main"
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.a05042.listviewproject.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="@+id/ListViewMain"
android:background="@android:color/holo_blue_bright" />
</RelativeLayout>
activity_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textAlignment="center" />
</LinearLayout>
JAVA:
public class MainActivity extends AppCompatActivity {
String[] myItems = {"Sony","Toshiba","Samsung","y","daniel","sold","sail"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MakeListView();
}
public void MakeListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_list,myItems);
ListView lv = (ListView)findViewById(R.id.ListViewMain);
lv.setAdapter(adapter);
}
}