简体   繁体   English

从SimpleCursorAdapter获取位置

[英]Get Position from SimpleCursorAdapter

Is there a way to do this? 有没有办法做到这一点? My specific purpose is that I am trying to populate a label from an edittext within my list that is populated by a simplecursoradapter. 我的特定目的是我试图从列表中的edittext填充标签,该列表由simplecursoradapter填充。 When I gather the information from the cursor is only gives me the value from the most recent item in the list. 当我从游标中收集信息时,仅提供了列表中最新项目的值。

Example: 例:

        lbs = (TextView)view.findViewById(R.id.GrainLbs);
    lbs.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            if(!popState){
                parentActivity.showPopup(v, mView, getId, String.valueOf(getLbs));
                Log.i("Current ID", "");
            }
            setPopState();
            return true;
        }   
    });

I'm trying to pass the value from the database to a method in my activity from within my custom simplecursoradapter (String.valueOf(getLbs)) which since i'm gathering this information by clicking on an object in the listview I thought it would automatically use the data from the specific list item. 我试图从我的自定义simplecursoradapter(String.valueOf(getLbs))内将值从数据库传递给活动中的方法,这是因为我通过单击列表视图中的对象来收集此信息,我认为这会自动使用特定列表项中的数据。 Is there a way I can get the list item position and then use info from the database based on the position? 有没有一种方法可以获取列表项的位置,然后根据该位置使用数据库中的信息?

If you are extending ListActivity , then you can override L istActivity.onListItemClick . 如果要扩展ListActivity ,则可以覆盖L istActivity.onListItemClick The second argument passed into that function is the position in the list, which is the same as the position in your SimpleCursorAdapter . 传递给该函数的第二个参数是列表中的位置,该位置与SimpleCursorAdapter的位置相同。

This turned out to be such a simple answer and I can't believe i've spent so much time trying to figure it out. 事实证明,这是一个简单的答案,我不敢相信我花了很多时间来解决这个问题。 Because I was collecting the value within an inside method (an onclicklistener) I needed to make my variable final, it was declared at the beginning of the class so it could be used throughout, and because of this I wasn't getting an exception, but I needed to make it final before it would pass the correct value for whatever reason. 因为我需要在一个内部方法(一个onclicklistener)中收集值,所以需要将变量定为final,所以在类的开头声明了它,以便可以在整个过程中使用它,因此,我没有遇到异常,但无论出于何种原因,我都需要将其最终确定,然后再传递正确的值。

So I guess if you bind your views within a customcursoradapter you can collect values for each individual listitem without knowing the position, it automatically knows this for you. 因此,我想如果您将视图绑定到customcursoradapter中,则可以在不知道位置的情况下为每个单独的listitem收集值,它会自动为您知道。

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

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