2012-04-10 65 views
0

我已经创建了这个代码,它在Listview中填充SQLite,但是我正在努力的是,我想根据用户从listview中选择什么来显示TOAST消息。我应该使用onClick方法吗?onclick在列表视图中的sqlite?

public void createTable(SQLiteDatabase mDb, String table) { 
     try { 
      mDb.execSQL("create table if not exists " 
        + table 
        + " (id integer primary key autoincrement, " 
        + "username text not null, birthday text not null,image text);"); 
     } catch (SQLException e) { 
      Toast.makeText(getApplicationContext(), "yes", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void insert(SQLiteDatabase mDb, String table) { 

        ContentValues values = new ContentValues(); 


    public void getAllData(String table) { 
     Cursor c = mDb.rawQuery("select * from " + table, null); 
     int columnsSize = c.getColumnCount(); 
     listData = new ArrayList<HashMap<String, Object>>(); 
     while (c.moveToNext()) { 
      HashMap<String, Object> map = new HashMap<String, Object>(); 
      for (int i = 0; i < columnsSize; i++) { 
       map.put("id", c.getString(0)); 
       map.put("username", c.getString(1)); 
       map.put("birthday", c.getString(2)); 
       map.put("image", c.getString(3)); 
      } 
      listData.add(map); 
     }   
    } 
    public boolean delete(SQLiteDatabase mDb, String table, int id) { 
     String whereClause = "id=?"; 
     String[] whereArgs = new String[] { String.valueOf(id) }; 
     try { 
      mDb.delete(table, whereClause, whereArgs); 
     } catch (SQLException e) { 
      Toast.makeText(getApplicationContext(), "هˆ é™¤و•°وچ®ه؛“ه¤±è´¥", 
        Toast.LENGTH_LONG).show(); 
      return false; 
     } 
     return true; 
    } 
} 

OnCreateContextMenuListener listviewLongPress = new OnCreateContextMenuListener(){ 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     final AdapterView.AdapterContextMenuInfo info = 
     (AdapterView.AdapterContextMenuInfo) menuInfo; 
     new AlertDialog.Builder(ListView_SqliteActivity.this) 
       .setTitle("عنوان1") 
       .setIcon(android.R.drawable.ic_dialog_info) 
       .setMessage("عنوان 2") 
       .setPositiveButton("وک¯", 
         new DialogInterface.OnClickListener() { 
          public void onClick(
            DialogInterface dialoginterface, int i) { 
           int mListPos = info.position; 
           HashMap<String, Object> map = listData 
             .get(mListPos); 
           int id = Integer.valueOf((map.get("id") 
             .toString())); 
           if (dao.delete(mDb, "student", id)) {          
            listData.remove(mListPos); 
            listItemAdapter.notifyDataSetChanged(); 
           } 
          } 
         }) 
       .setNegativeButton("هگ¦", 
         new DialogInterface.OnClickListener() { 
          public void onClick(
            DialogInterface dialoginterface, int i) { 

          } 
         }).show(); 
    } 
}; 

@Override 
public void finish() { 
    // TODO Auto-generated method stub 
    super.finish(); 
    mDb.close(); 
} 

}

回答

0

我想从您要实现Click事件为ListView你的问题不大,要显示吐司消息的点击。

因此,实施setOnItemClickListener为您的listview处理列表项点击。

list.setOnItemClickListener(new ListView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int i, long l) { 
      // do whatever you want 
     } 
    }); 
+0

我应该在哪里粘贴?你能告诉我吗? – Ali 2012-04-10 12:38:12

+0

粘贴在同一类 – 2012-04-10 12:39:05

+0

后面这一行:'list.setOnCreateContextMenuListener(listviewLongPress);' – 2012-04-10 12:40:14