I have the method show_list
that creates a List
from some strings, after adds the List
to ArrayAdapter
and binds the adapter to ListView
. Returns the ListView
created. How can I add the ListView
to MainActivity
and show it?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private View show_list() {
String[] data = {"Ionut","Eugeniu","George","Felix","Fazlidin"};
List<String> data_list = new ArrayList<>(Arrays.asList(data));
ArrayAdapter<String> data_adapter = new ArrayAdapter<>(this,R.layout.list_item,data_list);
ListView data_view = (ListView)this.findViewById(R.id.list_view);
data_view.setAdapter(data_adapter);
return data_view;
}
}
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"
tools:context="com.example.ionut.vremea2.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Ionut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item" />