0

カスタムを使用して名前のリストを作成しようとしていますListView。各行には、編集および削除ボタンがあります。

編集ボタンは正常に機能しますが、ユーザーが編集ボタンまたは削除ボタンを押した後にデータベースを更新する方法がわかりません。

これは私がやりたいことです:

  1. UI とデータベースから行を削除したい。

  2. ユーザーがオプション メニューのデータベース保存ボタンを押したときに、編集可能なテキストを保存したいと考えています。

これが私のコードです:

EditList.java [メイン]

package edit.list;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class EditLIST extends Activity {


    private AndroidSQLite mySQLiteAdapter ;

 final static String EXTRA_MESSAGE = "edit.list.message";


 public void onClick (View view) {

    Intent intent = new Intent(this,display.class);
    RelativeLayout relativeLayout = (RelativeLayout) view.getParent();

    TextView textView = (TextView) relativeLayout.findViewById(R.id.textView1);
//  TextView textView=(TextView) findViewById(R.id.textView1);
    String message = textView.getText().toString();

        intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);

 }

 public void delete (View view){
     //[what i have to write here]
 }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_list);
        ListView listContent = (ListView)findViewById(R.id.listView1);



        mySQLiteAdapter = new AndroidSQLite(this);
        mySQLiteAdapter.openToWrite();
        mySQLiteAdapter.deleteAll();

       mySQLiteAdapter.insert("umesh");
       mySQLiteAdapter.insert("ramesh");
       mySQLiteAdapter.insert("piyush");

      mySQLiteAdapter.close();

      mySQLiteAdapter = new AndroidSQLite(this);
      mySQLiteAdapter.openToRead();

        Cursor cursor = mySQLiteAdapter.queueAll();
        startManagingCursor(cursor);

        String[] from = new String[]{AndroidSQLite.KEY_FNAME};
        int[] to = new int[]{R.id.textView1};




        SimpleCursorAdapter cursorAdapter =
         new SimpleCursorAdapter(this, R.layout.button, cursor, from, to);
        listContent.setAdapter(cursorAdapter);
    }

    protected void onDestroy() {
        super.onDestroy();
    mySQLiteAdapter.close();
    }
    }

AndroidSQLite.java

package edit.list;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class AndroidSQLite {

public static final String MYDATABASE_NAME = "abncd";
public static final String MYDATABASE_TABLE = "zxc";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_FNAME = "FNAME";
public static final String KEY_CONTENT1 = "Content1";



//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_FNAME + " text not null);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public AndroidSQLite(Context c){
context = c;
}

public AndroidSQLite openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}

public AndroidSQLite openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}

public void close(){
sqLiteHelper.close();
}

public long insert(String fname){

ContentValues contentValues = new ContentValues();

contentValues.put(KEY_FNAME, fname);

return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}

public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}

public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_FNAME};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
  null, null, null, null, null);

return cursor;
}
public void delete(long id)
{
 sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null);   

}


 public void update_byID(int id, String v1)
 {
      ContentValues values = new ContentValues();
      values.put(KEY_CONTENT1, v1);          
      sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null);

 }
public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
  CursorFactory factory, int version) {
 super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
 // TODO Auto-generated method stub
 db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 // TODO Auto-generated method stub

}

}

}

表示.java

package edit.list;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class display extends Activity {
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);


                // Get the message from the intent
                Intent intent = getIntent();
                String message = intent.getStringExtra(EditLIST.EXTRA_MESSAGE);

                // Create the text view
                EditText editText = new EditText(this);
                editText.setTextSize(40);
                editText.setText(message);

                // Set the text view as the activity layout
                setContentView(editText);
                }
     public boolean onCreateOptionsMenu (Menu menu)
        {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_edit_list,menu);
            return true;
        }
     public boolean onOptionsItemSelected(MenuItem item)
     {  
            //int id=et1.getId();
            // Handle item selection
            switch (item.getItemId())
            {
                case R.id.save:
                    //[WHAT I HAVE 2 WRITE HERE]
                    finish();
                    return true;
                case R.id.cancel:
                    Toast.makeText(this, "Cancle clicked",Toast.LENGTH_SHORT).show();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
    }
}

ここにxmlファイル.. activity_edit_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false">

    </ListView>
</RelativeLayout>

ボタン.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:onClick="onClick"
        android:text="delete" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button1"
        android:onClick="Click"
        android:text="edit" />



    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button2"
        android:text=" " />

</RelativeLayout>

https://lh6.googleusercontent.com/-NfeEPAeYSDo/UCtlQQepw2I/AAAAAAAAA-I/SnAUytNSO-Q/s640/Capture.JPG

4

0 に答える 0