2011-10-20 4 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

내 대답을 편집했습니다. 그것이 도움이 될지 확실하지 않지만, 변화가 이루어지면 상황이 달라지고 더 나아질 것입니다. –

관련 문제