如何根据列表的位置从列表项目的数据库中获取项目

时间:2011-04-11 10:38:00

标签: android

我想根据位置获取列表视图中的值save .. Sir actualy我从NOTEPAD教程中获取帮助,在列表视图中添加新项目... 现在我想根据列表视图中的位置从onlist项目点击事件获取项目该怎么做.. 是不是可能先生请看我的代码在列表项上做什么点击忽略评论部分“在列表项目点击”

提前感谢您的帮助....

 public class planner extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private NotesDbAdapter mDbHelper;
    private Long mRowId;
    private ServerResponce AllList;
    // private DeviceListAdapter AllList;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listonruntime);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        saveState();
        fillData();

        registerForContextMenu(getListView());

        String responce = null ;

    }
    private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_BODY,NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_NUMBER};

        int[] to = new int[]{R.id.toptext,R.id.middletext,R.id.circle};

       SimpleCursorAdapter notes =   
                                    new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);

    setListAdapter(notes);

    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteNote(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

//      @Override
//    protected void onSaveInstanceState(Bundle outState) 
//    {
//        super.onSaveInstanceState(outState);
//        saveState();
//        outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
//    }

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
       // ServerResponce servresp=new ServerResponce();
       String responceId = Activity1.getData();
        String responceno = Activity2.getData();
        String city = cabbookingapplication.Selection;
        String area = cabbookingapplication.Selection2;
        String exactadd  = cabbookingapplication.Selection3;
        String nearby = cabbookingapplication.Selection4;
        private void saveState() {
        String title =("FROM LOC::"+city+","+area+","+exactadd+","+nearby);
        String body = ("TO LOC::"+city+","+area+","+exactadd+","+nearby);
        String number = (""+responceno+",,"+responceId);
            if (mRowId == null) {
            long id = mDbHelper.createNote(title, body, number);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateNote(mRowId, title, body,number);
            }
        }

    **@Override
    protected void onListItemClick(ListView l, View v, int position, long thisID)
    {
        super.onListItemClick(l, v, position, thisID);
        String resposponseid = Activity2.getData();

        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);




        //  Object o = this.getListAdapter().getItemId(position);
        //    String c = ((Cursor) notes).getString(position);
        //       String ssssss= c.getString(position);
       String device_name = (String) ((Cursor)getListAdapter()).getString(position);
//        Bundle bundle = new Bundle();
//        bundle.putString("NAME", device_name);

       // String device_name = (String) (planner.this). getListAdapter().getItem(position);
        //response.get(position);
        //ArrayList respCode<String> = new Arraylist<String>();
        // add("response");
        //String ssss[]={sss};
        // Object o = (String) (Notepadv3.this).getListAdapter().getItem(position);
        // String pen = o.toString();
          Toast.makeText(this, "this row responceid is= " +  device_name, Toast.LENGTH_LONG).show();
 //        Object o =  this.getListAdapter().getItem(position);


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();

    }

}

1 个答案:

答案 0 :(得分:0)

您有两种不同的选择。

第一个选项

onListItemClick()方法long thisID的最后一个参数是数据库中单击项的唯一ID。但是,只有当绑定到适配器的游标包含名为_id的列时,此id才可用,该列必须包含每个项的唯一ID。然后,您可以在数据库中查询具有此唯一ID的项目。不要将它与position参数混淆,后者只告诉你列表中项目的位置,这个值与数据库中项目的数据id无关。

第二个选项

当您获得当前所选项目的视图对象时,可以通过搜索包含设备名称的视图元素从视图中提取值。

protected void onListItemClick(ListView l, View v, int position, long thisID) {
     TextView title = (TextView) v.findViewById(R.id.middletext);      
     String deviceName = title.getText();
}