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

时间:2011-04-09 09:36:17

标签: android

我可以添加到数据库并列表作为列表视图。当我使用onListItemClick单击列表项时,我需要什么语句来获取值?

 package cabs.h;

import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
    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();
        fillData();
        registerForContextMenu(getListView());

        String responce = null ;
       // startManagingCursor(responce);
        //AllList = new ServerResponce(responce);
    }
    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();
        Object o = this.getListAdapter().getItemId(position);


       // String device_name = (String) (planner.this). getListAdapter().getItem(position);


        Toast.makeText(this, "this row responceid is= " + " " + keyword, Toast.LENGTH_LONG).show();


    }


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

    }

}

并在列表项上单击我想要根据行位置保存在列表项中的反向值 怎么做

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



          Toast.makeText(this, "this row responceid is= " + " " + keyword, Toast.LENGTH_LONG).show();


    }**

1 个答案:

答案 0 :(得分:2)

如果查看方法onListItemClick()的文档,则可以看到最后一个参数是所单击项的行ID。

  

此方法将在调用时调用   选择列表中的项目。   子类应该覆盖。子类   能打电话   getListView()。getItemAtPosition(位置)   如果他们需要访问数据   与所选项目相关联。

     

参数

     

l:发生点击的ListView

     

v:在ListView中单击的视图

     

position:列表中视图的位置

     

id:项目的行ID   点击

但是要获得正确的行id,传递给适配器的游标包含一个名为“ _id ”的行,表示表中每行的唯一ID非常重要。