忍者ブログ

Androidでアプリの開発をしていきます。まだ初心者。。。 JavaはOJC-Pを取得しました。 無事Androidの資格もGETです

<< 09  2024/10  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31    11 >>

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

個人的にメモしたいことが増えてしまい、連続投稿致します。
今回はListViewの作成についてです。
ListViewは何かと作成する機会が今後もあるでしょうし、ここにメモしておきます。

①まず枠組みとなるレイアウトの作成
res/layout/layout_main.xml←デフォルト作成のレイアウトファイル


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >
 
↓入力欄と保存ボタンの作成を行っています、TextViewは追加項目が発生していない場合のみ表示 
    <EditText 
        android:layout_height="wrap_content"
        android:layout_width="350dp"
        android:hint="@string/hintTxtItem"
        android:inputType="text"
        android:id="@+id/txtItem" />
    <Button 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/btnAdd"
        android:layout_toRightOf="@id/txtItem"
        android:text="@string/lblBtnAdd" />
    <TextView 
        android:id="@android:id/empty"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal"
        android:text="@string/txtEmpty"
        android:layout_below="@id/txtItem" />

↓ListViewの作成です。ここではマルチ選択を設定しています
    <ListView 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@android:id/list"
        android:layout_below="@id/txtItem"
        android:choiceMode="multipleChoice" >        
    </ListView>

android:idの種類はこちらのサイト様を参考にしました。ありがとうございます。
http://androyer.blogspot.jp/2011/05/androidid.html   
あんまり意識してなかったのと、資格取得にも意味についてまでは語られなかったので
とても参考になりました。   


↓削除ボタンの作成
 <Button 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/btnDel"
        android:text="@string/lblBtnDel"
        android:layout_alignParentBottom="true" />
  
</RelativeLayout>

ちなみに@stringはres/values/string.xmlで設定したリソースを使用しています。
直入力でもいいのですが、国際化や今後のためにもリソースからの参照がいいと思います。
個人的に。


②次にActivityの設定を行います。

import java.util.ArrayList;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

↓ここで1つ、当たり前かもしれませんが、必ず継承はListActivityを継承します。
ここで既存のまま、Activityクラスの継承をしているとのちのgetView()が使用できません。
(管理人はここでしばらく躓きました……orz)
public class MainActivity extends ListActivity {
 
ArrayList list = new ArrayList();
ArrayAdapter adapter;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);

↓ボタンの適用とArrayAdapterの設定
Button btnadd = (Button)findViewById(R.id.btnAdd);
Button btndel = (Button)findViewById(R.id.btnDel);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,list);

↓いわゆる保存ボタンを押したときの処理を実装
OnClickListener listener = new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
EditText edit = (EditText)findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
 
}
};
↓いわゆる削除ボタンを押したときの処理を実装
OnClickListener listenerDel = new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
 
for(int i = itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
 
}
};
↓ボタンに処理を実装しています
btnadd.setOnClickListener(listener);
btndel.setOnClickListener(listenerDel);
setListAdapter(adapter);
}
}
 
とまあ、こんな具合で入力したことを保存ボタンでリストに追加し、
チェックボタンで選択して、削除ボタンでそれらを削除するという処理を実装しました。

完成は下になります。



あくまで、一例ですので、ご参考までに。





拍手[0回]

PR

Post your Comment Post your Comment
Name
Title
E-mail
URL
Comment
Pass   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
プラグインタイトル カレンダー
09 2024/10 11
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
プラグインタイトル 最新CM
プラグインタイトル プロフィール
HN:
Hideaki
性別:
非公開
自己紹介:

プラグインタイトル バーコード
プラグインタイトル ブログ内検索
プラグインタイトル ブック検索
プラグインタイトル 訪問者数
Script:Ninja Blog  Design by: タイムカプセル
忍者ブログ [PR]