ListView中の項目削除する方法を教えでください

892 views
Skip to first unread message

roger

unread,
Sep 2, 2010, 10:11:29 PM9/2/10
to 日本Androidの会
皆さん こんにちは

rogerです、最近Androidアプリ開発していますが、
ListViewの使い方がちょっと難しい感じています。

今回実現したいのは、こんな感じ:
---------------------------------------------
String1.... ☆
String2... ×
---------------------------------------------
String3.... ☆
String4... ×
---------------------------------------------
String5.... ☆
String6... ×
---------------------------------------------

ListViewで自定義ArrayAdapterをセットして表示できますが、各項目について「☆」と「×」ボタンを押すと、DBに更新(削除)し、
ListViewも更新(削除)したい。
DB更新操作は問題解決しましたが、ListViewを更新、削除の方法を見つかりません。ご存知の方が居れば教えでもらいませんが?

以下は部分ソースです:
public class WordsAdapter extends ArrayAdapter{

private LayoutInflater mInflater;
private ArrayList items;
private Boolean isHistoryTab = false;

/**
* 該当tabを履歴tabを設定
* @param isHistoryTab
*/
public void setIsHistoryTab(Boolean isHistoryTab) {
this.isHistoryTab = isHistoryTab;
}

public WordsAdapter(Context context,int textViewResourceId,List
objects) {
super(context,textViewResourceId, objects);
mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = (ArrayList) objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

// ビューを受け取る
if (convertView == null) {
// 受け取ったビューがnullなら新しくビューを生成
convertView = mInflater.inflate(R.layout.list_at, null);
}

final View a = convertView;
// 表示すべきデータの取得
final DbTableBean item = (DbTableBean)items.get(position);
if (item != null) {
TextView oWords = (TextView) convertView.findViewById(R.id.oWords);
final CheckBox cStar = (CheckBox)
convertView.findViewById(R.id.star);
TextView tWords = (TextView) convertView.findViewById(R.id.tWords);
Button bDel = (Button) convertView.findViewById(R.id.bDelete);

//「☆」eventを設定
cStar.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//starの状態を変更処理
DataDao dao = new DataDao(a.getContext());
DbTableBean newBean= new DbTableBean();
newBean = item;
int checked = 0;
if(cStar.isChecked()){
checked = 1;
}
newBean.setStarFlag(checked);
//db更新
dao.saveData(newBean);
}

});

//「×」eventを設定
bDel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//ここで処理追加できないですか?
// Items.remove(item); を使いましたが、無理みた
い!
}
});

oWords.setText(item.getOriginalWords());
int flg = item.getStarFlag();
if(flg == 0){
cStar.setChecked(false);
}else{
cStar.setChecked(true);
}
tWords.setText(item.getTranslationWords());
}
return convertView;
}


}


以上、よろしくお願い致します。


androkun

unread,
Sep 3, 2010, 6:07:57 AM9/3/10
to 日本Androidの会

Array listの内容を更新(削除)した後で、
AdapterのnotifyDataSetChanged()を呼べば、ListViewの内容が更新されるかと思いますが、いかがでしょうか

roger

unread,
Sep 4, 2010, 2:53:15 AM9/4/10
to 日本Androidの会
本当ですね、androkunさん有難うございました!
Reply all
Reply to author
Forward
0 new messages