更新Listview中的项目并将信息存储在SQLite数据库中

时间:2018-10-19 17:40:55

标签: android sql sqlite listview

我正在做一个项目,我必须将数据存储在sql数据库中,然后在应用程序上显示数据库的相关信息。为此,我正在使用Listview。现在,问题是当我必须单击一个列表项中的一个按钮时,它应该自动更改列表项中一个textview的值并将其存储。 列表项的布局如下:

  @RunWith(PowerMockRunner.class)
    @PrepareForTest({WorkerParameters.class})
    public class SampleWorkerTest {

    @Mock
    Context context;

    WorkerParameters workerParams;

    Data inputData;

    SampleWorker worker;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        workerParams = PowerMockito.mock(WorkerParameters.class);

        inputData = SampleWorker.createData("data one", "data two");
        when(workerParams.getInputData()).thenReturn(inputData);

        worker = new SampleWorker(context, workerParams);
    }

    @Test
    public void testCreateWorkRequestFactoryMethod() {
        WorkRequest workRequest = SampleWorker.createWorkRequest("data one", "data two");
        WorkSpec workSpec = workRequest.getWorkSpec();

        // verify constraints
        assertThat(workSpec.constraints.getRequiredNetworkType(), is(equalTo(NetworkType.CONNECTED)));

        // verify input data
        assertThat(workSpec.input.getString(SampleWorker.KEY_DATA_1), is(equalTo("data one")));
        assertThat(workSpec.input.getString(SampleWorker.KEY_DATA_2), is(equalTo("data two")));
    }

    @Test
    public void testDoWorkSuccess() {
        Worker.Result result = worker.doWork();

        // verify business logic here

        assertThat(result, is(equalTo(Worker.Result.SUCCESS)));
    }
}

这是我制作的自定义光标适配器:

<?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>

0 个答案:

没有答案