読者です 読者をやめる 読者になる 読者になる

【android】ListViewひな型

試したりするときにとりあえずListView作ることが多いのでコピペ用にメモ

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ArrayList<Item> mItems;
    private ItemAdapter mAdapter;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mItems = new ArrayList<>();
        mItems.clear();
        mAdapter = new ItemAdapter(this, 0, mItems);

        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(mAdapter);

        setData();
    }

    private void setData() {
        for (int i = 0; i < 10; i++) {
            Item item = new Item();
            item.setTitle("タイトル " + String.valueOf(i+1));
            mItems.add(item);
        }
    }
}

class Item {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

class ItemAdapter extends ArrayAdapter<Item> {
    private LayoutInflater layoutInflater;

    public ItemAdapter(Context c, int id, ArrayList<Item> data) {
        super(c, id, data);
        this.layoutInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item,parent,false);
        }
        Item data = getItem(position);

        TextView title = (TextView)convertView.findViewById(R.id.title);
        title.setText(data.getTitle());

        return convertView;
    }
}

main_activity.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">

  <ListView
      android:id="@+id/list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"></ListView>
</RelativeLayout>

list_item.xml

<?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="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5sp"
        android:paddingTop="25sp"
        android:paddingBottom="15sp"
        android:textSize="24sp"/>
</LinearLayout>                                                                                                                                                                                                    
||<