在sqlite数据库中保存Listview数据

时间:2017-02-14 12:48:06

标签: android sqlite listview android-sqlite

我必须在ListView中为每一行添加2个按钮。按钮是添加和删除。当用户选择其中一个按钮时,应采取一些操作。 用户从datepicker中选择数据。我创建了保存按钮。当单击保存按钮时,listview中的数据以及该日期必须保存在Sqlite数据库中。我该怎么办? 以下是我的代码。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
    <ImageView
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:id="@+id/calender"
        android:background="@drawable/calendar"/>


    <EditText
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:hint="Enter Date..."
        android:layout_centerHorizontal="true"

        android:textColorHint="#992e28"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_toRightOf="@+id/calender"
        android:id="@+id/date"/>

    <ListView
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:id="@+id/my_listview"></ListView>


    <Button
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:id="@+id/save"
        android:text="Save"/>
</LinearLayout>


custom_list.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
    android:id="@+id/list_item_string"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:paddingLeft="8dp"
    android:textSize="18sp"
    android:textStyle="bold" />

    <Button
    android:id="@+id/delete_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:text="Delete" />

    <Button
    android:id="@+id/add_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/delete_btn"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:text="Add" />
</RelativeLayout>

MainActivity.java
package com.example.abc.lvtodb2;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends Activity {
    ListView lView;
    Button insert;

    EditText date;
    ImageView cal;

    DatePickerDialog datePickerDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initiate the date picker and a button
        date = (EditText) findViewById(R.id.date);
        cal = (ImageView) findViewById(R.id.calender);
        // perform click event on edit text
        cal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR);//current year
                int mMonth = c.get(Calendar.MONTH); // current month
                int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                // date picker dialog
                datePickerDialog = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                // set day of month , month and year value in the edit text
                                date.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);

                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }


        });


        //generate list
        ArrayList<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("item2");
        list.add("item3");
        list.add("item4");
        list.add("item5");

        insert=(Button)findViewById(R.id.save);

        //instantiate custom adapter
        CustomAdapter adapter = new CustomAdapter(list,MainActivity.this);

        //handle listview and assign adapter
         lView = (ListView)findViewById(R.id.my_listview);
        lView.setAdapter(adapter);


    }


}


CustomAdapter.java
package com.example.abc.lvtodb2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by ABC on 2/14/2017.
 */
public class CustomAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList<String> list = new ArrayList<String>();
    private Context context;

    public CustomAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_list, null);
        }

        //Handle TextView and display string from your list
        TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position));

        //Handle buttons and add onClickListeners
        Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
        Button addBtn = (Button)view.findViewById(R.id.add_btn);

        deleteBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //do something
                list.remove(position); //or some other task
                notifyDataSetChanged();
            }
        });
        addBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"Item added",Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
        });

        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

即使你尝试编码,你也要学习很多Android东西。

  1. 了解如何在表格中创建,插入,更新,删除行。 SqliteOpenHelper类。
  2. 开始使用Cursor Adaper
  3. 尝试使用ViewHolder模式在getView方法中缓存视图
  4. 您可能必须根据点击事件插入/更新项目。
  5. 我建议您搜索google或stackoverflow中的每个主题,看看该怎么做。没有人会帮你编写自己的代码。你得做。

    最佳资源:developer.android.com