Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Really simple sample application
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id="@+id/btn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:text="Add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="onAddButton"/>
<Button
android:text="Remove"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="onRemoveButton"/>
</LinearLayout>
<ListView
android:layout_above="@id/btn_container"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="labels">
<item>Foo</item>
<item>Bar</item>
<item>Hoge</item>
<item>Fuga</item>
</string-array>
</resources>
package com.hkurokawa.sampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private ListView list;
private ArrayAdapter<String> adapter;
private int gid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
ArrayList<String> labelsList = new ArrayList<>();
labelsList.addAll(Arrays.asList(getResources().getStringArray(R.array.labels)));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, labelsList);
list.setAdapter(adapter);
}
public void onAddButton(View view) {
adapter.add("Item " + gid++);
}
public void onRemoveButton(View view) {
if (adapter.getCount() > 0) {
adapter.remove(adapter.getItem(adapter.getCount() - 1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.