繁体   English   中英

使ListView项目可点击麻烦

[英]Making ListView Items Clickable Trouble

我的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>

onListItemClick您获得了位置并将其从动态数组(向量或列表)中Remove ,就像vector.remove(position);一样vector.remove(position); 。如果要从数据库中删除它,也要从数据库中删除。之后,您只需编写...

adapter.notifyDataSetChanged();

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

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

那是最简单的方法:)

尽管我认为您可以使用长按而不是普通按。 只是这些项目不会被意外删除

我在我的Apps中所做的是重写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); 


    } 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM