Обновление элемента в ListView и сохранение информации в базе данных SQLite - PullRequest
0 голосов
/ 19 октября 2018

Я делаю проект, в котором мне нужно хранить данные в базе данных sql, а затем отображать соответствующую информацию базы данных в приложении.Для этого я использую Listview.Теперь проблема в том, что когда мне нужно нажать одну кнопку в одном элементе списка, он должен автоматически изменить значение одного текстового представления в элементе списка и сохранить его.Макет элемента списка выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item in the list of pets -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:orientation="horizontal"
android:padding="16dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginBottom="8dp">

    <TextView
        android:id="@+id/name_catalog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textColor="#263238"
        />

</LinearLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Quantity"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="#263238"
            />

        <TextView
            android:id="@+id/quantity_catalog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="#263238"
            tools:text="1111"
            android:layout_marginLeft="16dp"
            />


    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textColor="#263238"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/price_catalog"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textColor="#263238"
        android:layout_marginLeft="16dp"
        tools:text="1"

        />



</LinearLayout>

</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <Button
        android:id="@+id/sell_button_catalog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:backgroundTint="#FFC107"
        android:onClick="sellQuantity"
        android:text="Sell"
        android:textColor="#eceff1"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textAllCaps="true"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

Это пользовательский адаптер курсора, который я сделал:

package org.graystranger.inventoryapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.graystranger.inventoryapp.data.InventoryContracts;
import org.graystranger.inventoryapp.data.InventoryContracts.InventoryEntry;
import org.w3c.dom.Text;

public class InventoryCursorAdapter extends CursorAdapter {
public static final String LOG_TAG = 
InventoryCursorAdapter.class.getSimpleName();



/**
 * Constructs a new {@link InventoryCursorAdapter}.
 *
 * @param context The context
 * @param c       The cursor from which to get the data.
 */
public InventoryCursorAdapter(Context context, Cursor c) {
    super(context, c, 0 /* flags */);
}

/**
 * Makes a new blank list item view. No data is set (or bound) to the views         yet.
 *
 * @param context app context
 * @param cursor  The cursor from which to get the data. The cursor is already
 *                moved to the correct position.
 * @param parent  The parent to which the new view is attached to
 * @return the newly created list item view.
 */
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}

/**
 * This method binds the inventory data (in the current row pointed to by cursor) to the given
 * list item layout. For example, the name for the current item can be set on the name TextView
 * in the list item layout.
 *
 * @param view    Existing view, returned earlier by newView() method
 * @param context app context
 * @param cursor  The cursor from which to get the data. The cursor is already moved to the
 *                correct row.
 */
@Override
public void bindView(final View view, Context context, final Cursor cursor) {
    TextView nameTextView  = view.findViewById(R.id.name_catalog);
    TextView priceTextView = view.findViewById(R.id.price_catalog);
    TextView quantityTextView = view.findViewById(R.id.quantity_catalog);

    int nameColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_INVENTORY_NAME);
    int priceColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_INVENTORY_PRICE);
    int quantityColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_INVENTORY_QUANTITY);

    String itemName = cursor.getString(nameColumnIndex);
    int itemPrice = cursor.getInt(priceColumnIndex);
    int itemQuantity = cursor.getInt(quantityColumnIndex);


    nameTextView.setText(itemName);
    priceTextView.setText(Integer.toString(itemPrice));
    quantityTextView.setText(Integer.toString(itemQuantity));


    //When the sell button is clicked, the quantity should decrease by 1 --??

}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...