2016-11-01 3 views
2

CursorAdapterを使用してデータを取得するためにデータベースを読み取るListViewが入力されています。まだ援助の答えはありません - カスタムCursorAdapterを使用して作成されたListViewのEditTextsからデータを読み取る方法?

ListItemには、上記のデータが読み込まれたTextViewと、そこからデータを読み込み、その中の値でデータベースを更新する2つのEditTextsが含まれています。ここで

がupdater_list_item.xmlです:

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

     <TextView 
      android:id="@+id/good_name" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/height_of_list_item" 
      android:paddingLeft="@dimen/padding" 
      android:gravity="center" 
      tools:text="Frozen Chicken"/> 

     <EditText 
      android:id="@+id/sold_edit_text" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/height_of_list_item" 
      android:paddingLeft="@dimen/padding" 
      android:gravity="center" 
      android:inputType="number" 
      android:hint="@string/hint_remove_quantity"/> 

     <EditText 
      android:id="@+id/received_edit_text" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/height_of_list_item" 
      android:paddingLeft="@dimen/padding" 
      android:gravity="center" 
      android:inputType="number" 
      android:hint="@string/hint_add_quantity"/> 

</LinearLayout> 

のOnCreate:

// Find the views 
    ListView list = (ListView) findViewById(R.id.list_updater); 
    TextView textView = (TextView) findViewById(R.id.updater_empty_view); 

    // Set the empty view 
    list.setEmptyView(textView); 

    // Set the ListView to the custom CursorAdapter 
    mAdapter = new UpdaterCursorAdapter(this, null); 
    list.setAdapter(mAdapter); 

    // the loader requests the data from the database and add it to the adapter 
    getLoaderManager().initLoader(STORE_LOADER, null, this); 

編集: ここでは、カスタムCursorAdapterからデータを取得する方法

public class UpdaterCursorAdapter extends CursorAdapter { 

    public UpdaterCursorAdapter(Context context, Cursor c) { 
     super(context, c, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
    TextView nameTextView = (TextView) view.findViewById(R.id.good_name); 
    EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text); 
    EditText receivedEditText = (EditText) view.findViewById(R.id.received_edit_text); 

    int nameColumnIndex = cursor.getColumnIndex(StoreEntry.COLUMN_GOOD_NAME); 

    String goodName = cursor.getString(nameColumnIndex); 

    nameTextView.setText(goodName); 
    } 
} 

ためのコードがありますそれぞれの2つのEditTextsItemListView?!

注:MenuItemをクリックすると、データがデータベースに保存されますので、onItemClickListenerまたはaddTextChangedListenerは実行されません。

+0

これはアダプタに含まれます。あなたのアダプターコードを掲示すれば、私たちはあなたを助けることができるでしょう。 – JDenais

+0

私は投稿しました.. –

+0

http://stackoverflow.com/questions/23716432/cursoradapter-in-listview?rq=1 –

答えて

0

あなたの編集テキストをアダプタに追加してから、textWatcherを追加し、いつものようにデータベースを更新するだけです。

public class UpdaterCursorAdapter extends CursorAdapter { 
    public UpdaterCursorAdapter(Context context, Cursor c) { 
     super(context, c, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView nameTextView = (TextView) view.findViewById(R.id.good_name); 
     int nameColumnIndex = cursor.getColumnIndex(StoreContract.StoreEntry.COLUMN_GOOD_NAME); 
     String goodName = cursor.getString(nameColumnIndex); 
     nameTextView.setText(goodName); 

     EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text); 
     //Set the soldEditText to whatever column your data is in the database same as text view. 
     soldEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

       // TODO Auto-generated method stub 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       //This is where you would save the new text your database just 
      } 
     }); 
    } 
} 
+0

これは、Save 'MenuItem'をクリックするとデータベース全体が更新されるのに役立ちません。 –

関連する問題