使ListView项目可点击麻烦

时间:2012-02-28 09:36:09

标签: java android mysql listview android-listview

我的Listview获取保存在本地数据库中的信息并显示信息。我正在改变点击listview项目时会发生什么。我想删除它们,在使用我的代码时,有人可以帮我解决这个问题吗?提前谢谢!

public class Notepad extends ListActivity {
    public static final int INSERT_ID = Menu.FIRST;
    EditText notes;
    Button add;
    ListView lv;
    String currentDateTimeString = DateFormat.getDateInstance().format(
            new Date());

    private NotesDbAdapter mDbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notepad_list);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
        Button add = (Button) findViewById(R.id.addNote);
        // ListView lv = (ListView) findViewById(R.id.list);

        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                createNote();

            }
        });
        // lv.setOnItemClickListener(new OnItemClickListener() {
        //
        // public void onItemClick(AdapterView<?> parent, View view,
        // int position, long id) {
        // // When clicked, show a toast with the TextView text
        //
        // try {
        // Toast.makeText(getApplicationContext(),
        // ((TextView) view).getText(), Toast.LENGTH_SHORT)
        // .show();
        //
        // } catch (ClassCastException e) {
        // Toast.makeText(getApplicationContext(), "Error",
        // Toast.LENGTH_SHORT).show();
        // }
        //
        // };
        //
        // });
    }

    private void createNote() {
        EditText notes = (EditText) findViewById(R.id.note);
        String noteName = notes.getText().toString();
        Calendar c = Calendar.getInstance();
        int seconds = c.get(Calendar.SECOND);
        int minutes = c.get(Calendar.MINUTE);
        int hour = c.get(Calendar.HOUR);
        mDbHelper.createNote(noteName + " Entered at " + hour + ":" + minutes
                + ":" + seconds, "");

        fillData();
    }

    private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = mDbHelper.fetchAllNotes();
        startManagingCursor(c);

        String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }
}

我的ListView:

   <ListView
        android:id="@id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="402dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/note" >
    </ListView>

3 个答案:

答案 0 :(得分:1)

onListItemClick您获得了位置,Remove来自动态数组(向量或列表),如vector.remove(position);。如果您想从数据库中删除它,也从数据库中删除。之后你只是写.....

adapter.notifyDataSetChanged();

答案 1 :(得分:0)

您在ListView上添加onItemClickListener就像在按钮上添加一样。 如果他们单击某个项目,则使用sql查询删除本地数据库中的该项目。我假设您可以选择数据,您可以使用SQL删除它。

之后只需重新初始化列表。

这是最简单的方法:)

虽然我认为你可以更好地使用长按然后正常按压。 只是那些项目不会被意外删除

答案 2 :(得分:0)

我在我的应用中执行的操作是覆盖SimpleCursorAdapter.setViewBinder()以使用来自数据库的ID(_ID)在ListView中设置视图标记,并从setOnItemClickListener()中的数据库中删除此ID并刷新适配器。像这样:

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, 
                R.layout.notes_row, c, from, to); 

    notes.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 

public boolean setViewValue(View view, Cursor cursor, int column) 
{ 
TextView tv = (TextView) view; 
view.setTag=cursor.getInt(cursor.getColumnIndex ("_id")); // You need to include the _id in the query
tv.setText(String.Valueof(cursor.getInt(cursor.getColumnIndex (NotesDbAdapter.KEY_TITLE ))));
return true; 
} 
});

         lv.setOnItemClickListener(new OnItemClickListener() { 

         public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

         TextView tv=(TextView) view;
     String ID=view.getTag();
    // Delete ID from the DB
    notes.notifyDataSetChanged(); 

         }; 

         });    
    setListAdapter(notes); 


    } 
}