删除由SQlite使用Dialogfragment填充的listview项

时间:2016-07-06 08:54:13

标签: android sqlite listview

我是Android编程的新手,我已经被困4天试图找出这个。

App结构

1)主要活动 - Mainactivity包含以下功能     a)ListView从SQLite数据库接收信息(" TASKDATABASE")并填充Class任务的对象     b)在Action栏中添加图标,链接到对话片段" CreateTask" - 这会创建一个新任务     c)TextView,它捕获Listview中的任务数量(即listview项目的数量)     d)通过单击listview并调用另一个Dialogfragment" TaskOptions"

来删除任务的选项

2)课程任务 3)类TaskDatabase,它添加了一个名为" TASKDATA"的SQLite数据库。 4)DialogFragment CreateTask链接到Mainactivity操作栏中的添加按钮 - 这将创建一个新任务 5)DialogFragment Taskoptions,在MainActivity中单击ListView中的项目时调用

问题 - 我无法通过调用DialogFragment Taskoptions从ListView中删除项目!没有错误消息。物品没有被删除! Evrything正常工作!

以下是代码 的 MainActivity

package com.example.task;

import android.app.DialogFragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;

import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements CreateTask.CreateNewTask,TaskOptions.onDelete {
private SQLiteDatabase db;
private Cursor cursor;
private ListView listView;
private TextView textView2;


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

    SQLiteOpenHelper taskDatabase = new TaskDatabase(this);
    db = taskDatabase.getWritableDatabase();
    cursor = db.query("TASKDATA", new String[]{"_id", "TODAY"}, null, null, null, null, null);
    CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            cursor,
            new String[]{"TODAY"},
            new int[]{android.R.id.text1},
            0);
    listView = (ListView) findViewById(R.id.listView);

    listView.setAdapter(cursorAdapter);
    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int y=(int)id;
            DialogFragment taskOptions = new TaskOptions();
            Bundle args=new Bundle();
            args.putInt("id",y);
            taskOptions.setArguments(args);
            taskOptions.show(getFragmentManager(),"");
        }
    };
    listView.setOnItemClickListener(onItemClickListener);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView2.setText(String.valueOf(cursor.getCount()));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.create_new:
            CreateTask createTask = new CreateTask();
            createTask.show(getFragmentManager(), "");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public void createNewTask(Task task) {
    String task1 = task.getDescription();
    ContentValues contentValues = new ContentValues();
    contentValues.put("TODAY", task1);
    SQLiteOpenHelper taskDatabase = new TaskDatabase(this);
    db = taskDatabase.getWritableDatabase();
    db.insert("TASKDATA", null, contentValues);
    cursor = db.query("TASKDATA", new String[]{"_id", "TODAY"}, null, null, null, null, null);
    CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            cursor,
            new String[]{"TODAY"},
            new int[]{android.R.id.text1},
            0);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(cursorAdapter);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView2.setText(String.valueOf(cursor.getCount()));


}

@Override
protected void onDestroy() {
    super.onDestroy();
    db.close();
    cursor.close();
}

@Override
public void onDeleteItem(int m) {
    SQLiteOpenHelper taskDatabase = new TaskDatabase(this);
    db = taskDatabase.getWritableDatabase();
            db.delete("TASKDATA", "_id=?", new String[]{String.valueOf(m)});
    cursor = ((SimpleCursorAdapter)listView.getAdapter()).getCursor();

    CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            cursor,
            new String[]{"TODAY"},
            new int[]{android.R.id.text1},
            0);
    listView.setAdapter(cursorAdapter);**/
     textView2 = (TextView) findViewById(R.id.textView2);
     textView2.setText(String.valueOf(cursor.getCount()));

    }




}

DialogFragment - TaskOptions

package com.example.todoo;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class TaskOptions extends DialogFragment {
private int m ;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.task_options,null);
    m=getArguments().getInt("num");
    ListView listView2 = (ListView)v.findViewById(R.id.listView2);
    listView2.setOnItemClickListener(onItemClickListener);

    return v;
}
AdapterView.OnItemClickListener onItemClickListener= new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position==1){
            MainActivity activity=(MainActivity)getActivity();
            activity.onDeleteItem(m);
            dismiss();
        }
    }
};
public interface onDelete {
    void onDeleteItem(int m);

}

}

package com.example.task;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class TaskOptions extends DialogFragment {
private int m ;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.task_options,null);
    m=getArguments().getInt("num");
    ListView listView2 = (ListView)v.findViewById(R.id.listView2);
    listView2.setOnItemClickListener(onItemClickListener);

    return v;
}
AdapterView.OnItemClickListener onItemClickListener= new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position==1){
            MainActivity activity=(MainActivity)getActivity();
            activity.onDeleteItem(m);
            dismiss();
        }
    }
};
public interface onDelete {
    void onDeleteItem(int m);

}

}

类TaskDatabase

package com.example.task;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TaskDatabase extends SQLiteOpenHelper {

private final static String DB_NAME = "TASKDATA";
private final static int DB_VERSION = 1;

public TaskDatabase(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
           db.execSQL("CREATE TABLE TASKDATA ( "
                 +"_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                   +"TODAY TEXT);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


}

DialogFragment CreateTask

package com.example.task;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

public class CreateTask extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.create_task, container, false);
    ImageButton imageButton=(ImageButton)view.findViewById(R.id.imageButton);
    imageButton.setOnClickListener(onClickListener);
        return view;
    }

private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText editText = (EditText) getView().findViewById(R.id.editText);
        String newTask = String.valueOf(editText.getText());
        Task task = new Task(newTask);
        task.setDescription(newTask);
        MainActivity activity1 = (MainActivity) getActivity();
        activity1.createNewTask(task);
        dismiss();
    }

};

public interface CreateNewTask{
     void createNewTask(Task task);

}

}

请让我知道我哪里出错!

此致 AJ

0 个答案:

没有答案