如何通过自定义对话框更新数据

时间:2016-08-12 23:07:49

标签: android

我希望用户使用此对话框更新字符串。

我该怎么做?
我的数据库类中已有更新方法,但我不确定如何在对话框中实现更新

数据库类中的更新方法

public boolean updateData(String id,String name,String quantity,String category,String importance ) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, id);
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, quantity);
    contentValues.put(COL_4, category);
    contentValues.put(COL_5, importance);
    db.update(Table_Name,contentValues,"id =?",new String[]{id});
            return true;

MyDialog课程

package ie.example.artur.projectrepeat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Created by family on 12/08/2016.
 */
public class MyDialog extends DialogFragment{
    Button btn;
    EditText nameEditText,CategoryEditText,QuantityeditText,ImportanceeditText,Edit_Id;
    LayoutInflater inflater;
    DatabaseClass myDb;

    View v;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        inflater = getActivity().getLayoutInflater();
        v= inflater.inflate(R.layout.input,null);



        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(v).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myDb.updateData( Edit_Id.getText().toString(),nameEditText.getText().toString(), QuantityeditText.getText().toString(),
                        CategoryEditText.getText().toString(), ImportanceeditText.getText().toString());
            }





        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });




        return builder.create();


    }


}

EditActivity

public class Edit_Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DatabaseClass  database;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    Dialog d;
    EditText editText_name,editText_Quantity,editText_Category,editTextId,editText_Number;


    Button updateBtn;
    EditText nameEditText;


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



        listView = (ListView) findViewById(R.id.list_view);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        listView.setOnItemClickListener(this);
        database = new DatabaseClass(getApplicationContext());

        sqLiteDatabase = database.getReadableDatabase();

        Cursor cursor=database.getInformation(sqLiteDatabase);

        if (cursor.moveToFirst()) {
            do {

                String id, product_name, category;
                id = cursor.getString(0);
                product_name = cursor.getString(1);
                category = cursor.getString(2);
                DataProvider dataProvider = new DataProvider(id, product_name, category);
                listDataAdapter.add(dataProvider);
            } while (cursor.moveToNext()


                    );

        }

    }

    public void loginMethod() {
        // Create an instance of the dialog fragment and show it
        MyDialog dialog = new MyDialog();
        dialog.show(getFragmentManager(),"my_dialog");
    }






    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position , long id) {
        TextView tv = (TextView) view.findViewById(R.id.product_id);



        sqLiteDatabase = database.getReadableDatabase();

        loginMethod();

}}

0 个答案:

没有答案