O_Casper_ne 2013-04-19 05:37 采纳率: 7.7%
浏览 2842
已采纳

ListView Items 的点击问题

ListView获取信息保存在本地数据库然后再显示信息。我想点击listview的items时删除它们,如何实现?

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条回答 默认 最新

  • Baby_Bonnie 2013-04-23 03:00
    关注

    在程序中重写SimpleCursorAdapter.setViewBinder(),使用数据库中的 ID 来设置 ListView 里 Views 的 Tag,删除 setOnItemClickListener() 中数据库的 ID, 并刷新 Adapter。

     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); 
        } 
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误