2011-10-20 13 views
0

リストビューレイアウトにチェックボックスを追加しようとしています。私は様々なフォーラムの投稿を見てきましたが、私はまだ物事を考え出すのに苦労しています。まず、チェックボックスのクリックをキャプチャできません。次に、各テキストボックスを各動的リストアイテムにどのようにマッピングするのかよく分かりません。リストビューにチェックボックスを追加する際の問題

WaysToSaveList.javaここ

waystosave_list.xmlファイルです::ここで(http://www.vogella.de/articles/AndroidSQLite/article.html由来)コードです

WaysToSaveActivity.javaファイル

public class WaysToSaveActivity extends Activity { 
    private EditText mTitleText; 
    private EditText mBodyText; 
    private Long mRowId; 
    private WaysToSaveDbAdapter mDbHelper; 
    private Spinner mCategory; 

    @Override 
    protected void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     mDbHelper = new WaysToSaveDbAdapter(this); 
     mDbHelper.open(); 
     setContentView(R.layout.waystosave); 
     mCategory = (Spinner) findViewById(R.id.category); 
     mTitleText = (EditText) findViewById(R.id.waystosave_edit_summary); 
     mBodyText = (EditText) findViewById(R.id.waystosave_edit_description); 
     Button confirmButton = (Button) findViewById(R.id.waystosave_edit_button); 
     mRowId = null; 
     Bundle extras = getIntent().getExtras(); 
     mRowId = (bundle == null) ? null : (Long) bundle  
     .getSerializable(WaysToSaveDbAdapter.KEY_ROWID); 
     if (extras != null) { 
      mRowId = extras.getLong(WaysToSaveDbAdapter.KEY_ROWID); 
     } 

     populateFields(); 

     confirmButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
       setResult(RESULT_OK); 
       finish(); 
      } 
     }); 
    } 

    private void populateFields() { 
     String strChecked = null; 
     Log.v("AppStatus", "Now entering populateFields"); 
     if (mRowId != null) { 
      Cursor todo = mDbHelper.fetchWaysToSave(mRowId); 
      startManagingCursor(todo); 
      String category = todo.getString(todo 
      .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_CATEGORY)); 
     for (int i = 0; i < mCategory.getCount(); i++) { 
      String s = (String) mCategory.getItemAtPosition(i); 
      if (s.equalsIgnoreCase(category)) { 
       mCategory.setSelection(i); 
      } 
     } 

     mTitleText.setText(todo.getString(todo  
     .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_SUMMARY))); 
     mBodyText.setText(todo.getString(todo 
     .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_DESCRIPTION))); 
     } 
    } 

    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     saveState(); 
     outState.putSerializable(WaysToSaveDbAdapter.KEY_ROWID, mRowId); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     saveState(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     populateFields(); 
    } 

    private void saveState() { 
     String category = (String) mCategory.getSelectedItem(); 
     String summary = mTitleText.getText().toString(); 
     String description = mBodyText.getText().toString(); 
     String checked = "-1"; 
     if (mRowId == null) { 
      long id = mDbHelper.createWaysToSave(category, summary, description, 
      checked); 
      if (id > 0) { 
       mRowId = id; 
     } 
    } 

Waystosave_row.xmlをファイル

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

<ImageView 
    android:id="@+id/icon" 
    android:src="@drawable/addwaytosave" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="8px" 
    android:layout_height="40px" 
    android:layout_marginTop="8px" 
    android:layout_width="30px"> 
</ImageView> 

<TextView 
    android:text="@+id/TextView01" 
    android:layout_height="wrap_content" 
    android:id="@+id/label" 
    android:textSize="20px" 
    android:layout_marginTop="6px" 
    android:layout_width="wrap_content" 
    android:textColor="@color/black"> 
</TextView> 

<CheckBox 
    android:id="@+id/check" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true"> 
</CheckBox> 

</LinearLayout> 

waystosave_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout style="@style/TitleBar"> 
     <ImageButton style="@style/TitleBarAction" 
      android:contentDescription="@string/description_home" 
      android:src="@drawable/title_home" 
      android:onClick="onClickHome" /> 
     <ImageView style="@style/TitleBarSeparator" /> 
     <TextView style="@style/TitleBarText" /> 
     <ImageButton style="@style/TitleBarAction" 
      android:contentDescription="@string/description_about" 
      android:src="@drawable/about" 
      android:onClick="onClickAbout" /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14px" 
     android:text="What are ways you save energy? Press the 'Menu' button to insert 
     your tip." /> 
    <ListView 
     android:id="@android:id/list" 
     android:textSize="18px" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ListView> 
    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="18px" 
     android:text="@string/no_waystosave" />  
</LinearLayout> 

WaysToSaveDbAdapter.java

public class WaysToSaveDbAdapter { 
    // Database fields 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_CATEGORY = "category"; 
public static final String KEY_SUMMARY = "summary"; 
public static final String KEY_DESCRIPTION = "description"; 
public static final String KEY_CHECKED = "checked"; 
private static final String DATABASE_TABLE = "todo"; 
private Context context; 
private SQLiteDatabase database; 
private WaysToSaveDbHelper dbHelper; 

public WaysToSaveDbAdapter(Context context) { 
    this.context = context; 
} 

// http://www.vogella.de/articles/AndroidSQLite/article.html 

public WaysToSaveDbAdapter open() throws SQLException { 
    dbHelper = new WaysToSaveDbHelper(context); 
    database = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    dbHelper.close(); 
} 

public long createWaysToSave(String category, String summary, String description, 
    String checked) { 
    ContentValues initialValues = createContentValues(category, summary, 
    description, checked); 
     return database.insert(DATABASE_TABLE, null, initialValues); 
} 

public boolean updateWaysToSave(long rowId, String category, String summary, 
String description, String checked) { 
    ContentValues updateValues = createContentValues(category, summary, 
    description, checked); 
    return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" 
    + rowId, null) > 0; 
} 

public boolean deleteWaysToSave(long rowId) { 
    return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

public Cursor fetchAllWaysToSave() { 
    return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, 
    KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION, KEY_CHECKED }, null, null, null, 
    null, null); 
} 

public Cursor fetchWaysToSave(long rowId) throws SQLException { 
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] { 
KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION, KEY_CHECKED }, 
KEY_ROWID + "=" + rowId, null, null, null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 

     return mCursor; 
} 

private ContentValues createContentValues(String category, String summary, String 
    description, String checked) { 
    ContentValues values = new ContentValues(); 
    values.put(KEY_CATEGORY, category); 
    values.put(KEY_SUMMARY, summary); 
    values.put(KEY_DESCRIPTION, description); 
    values.put(KEY_CHECKED, checked); 
    return values; 
} 
} 

WaysToSaveDbHelper.javaファイルをファイル

public class WaysToSaveDbHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "applicationdata"; 
private static final int DATABASE_VERSION = 1; 

// Database creation sql statement 
private static final String DATABASE_CREATE = "create table todo (_id integer 
    primary key autoincrement, " + "category text not null, summary text not null, 
    description text not null, checked text not null);"; 

public WaysToSaveDbHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, 
int newVersion) { 
    database.execSQL("DROP TABLE IF EXISTS todo"); 
    onCreate(database); 
} 
} 

答えて

0

あなたは基本的に独自のリストアダプタを作成する必要があるとしています。カーソルを使用しているので、CursorAdapterクラスをサブクラス化する必要があります。あなたのbindViewメソッドでは、次のようなことを行います:

public abstract void bindView (View view, Context context, Cursor cursor){ 

    //assign onClick listener to the checkbox 
    CheckBox checkBox = (Checkbox)view.findViewById(R.id.check); 
    //assign an onClickListener to the checkbox 
    checkbox.setOnClickListener(new View.onClickListener(){ 
    public void onClick(View view){ 
     //do what ever you want here 
    } 
    }); 

    TextView label = (TextView)view.findViewById(R.id.label); 
    //assign whatever lable you want. remeber, you have the cursor pointing 
    //to what this row is supposed to be displaying. 
} 
+0

ありがとうございました。これを動作させることができませんでした。代わりに他の実装オプションを検討しています。カスタムアダプターを使用しようとすると、アプリケーションがクラッシュしました。 – user836200

+0

私は私の答えを編集しました。それが助けになるかどうかは確信していませんが、変化があれば、それを働かせることができれば、今は違うものになるはずです。 –

関連する問題