Skip to content

Instantly share code, notes, and snippets.

@hyoromo
Created November 27, 2009 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyoromo/243833 to your computer and use it in GitHub Desktop.
Save hyoromo/243833 to your computer and use it in GitHub Desktop.
ListView04
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#ffff0000</color>
</resources>
package jp.ne.hyoromo.android.DemoView04;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* シンプルListView04<br>
* ListViewの追加と削除ができる。
*
* @author hyoromo
*/
public class DemoView04 extends Activity implements OnClickListener {
private static ListView mListView;
private static Context mContext;
private static AdapterEx mAdapter;
private static int mCount;
// アダプター設定リスト
private String[] mData = { "hoge01", "hoge02", "hoge03", "hoge04", "hoge05", "hoge06", "hoge07", "hoge08",
"hoge09", "hoge10", "hoge11", "hoge12", "hoge13", "hoge14", "hoge15", "hoge16", "hoge17", "hoge18",
"hoge19" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
// ボタンにリスナー設定
Button btn01 = (Button) findViewById(R.id.Button01);
btn01.setOnClickListener(this);
Button btn02 = (Button) findViewById(R.id.Button02);
btn02.setOnClickListener(this);
// 独自アダプターの作成
mAdapter = new AdapterEx();
// リストビューの作成&アダプター設定
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mAdapter);
// リストに追加していく
ListData listData = new ListData();
for (mCount = 0; mCount < mData.length / 2; mCount++) {
listData = new ListData();
listData.text = mData[mCount];
listData.icon = R.drawable.icon;
mAdapter.add(listData);
}
}
/**
* ArrayAdapterを拡張したクラス。
*/
private class AdapterEx extends ArrayAdapter<ListData> {
private final LayoutInflater mInflater;
public AdapterEx() {
super(mContext, 0);
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// 画面に表示される毎に呼び出される
@Override
final public View getView(int position, View convertView, ViewGroup parent) {
ListStorage listStorage;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.row, parent, false);
listStorage = new ListStorage();
listStorage.image = (ImageView) row.findViewById(R.id.ImageView01);
listStorage.text = (TextView) row.findViewById(R.id.TextView01);
row.setTag(listStorage);
} else {
listStorage = (ListStorage) convertView.getTag();
}
ListData listData = getItem(position);
listStorage.image.setImageResource(listData.icon);
listStorage.text.setText(listData.text);
return row;
}
private class ListStorage {
ImageView image;
TextView text;
}
}
/**
* Listの中身
*/
public final class ListData {
public String text;
public int icon;
}
@Override
public void onClick(View v) {
ListData data;
switch (v.getId()) {
case R.id.Button01:
// 追加
data = new ListData();
data.icon = R.drawable.icon;
data.text = mData[mCount++];
mAdapter.add(data);
break;
case R.id.Button02:
// 削除
data = mAdapter.getItem(--mCount);
mAdapter.remove(data);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true"
android:layout_weight="1">
</ListView>
<LinearLayout
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<Button
android:id="@+id/Button01"
android:text="追加"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical" >
</Button>
<Button
android:id="@+id/Button02"
android:text="削除"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical" >
</Button>
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment