getItemIdAtPosition() in listview not showing

早过忘川 提交于 2020-01-25 00:26:08

问题


I need to get getItemIdAtPosition() from listView to get the id of the records from sqlite database. When i click on the listView on any item, it always shows "null" not showing the id.

this is the code:

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

               int id= ((int) listView.getItemIdAtPosition(i));

                //create our intent, as per usual
                Intent intent=new Intent(Tutorial10.this, Activity2.class);

                intent.putExtra(ID_EXTRA, id);
                startActivity(intent);
            }
        });

This activity2.java

public class Activity2 extends Activity{

String passedVar=null;
private TextView passedView=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act2);

    //Get our passed variable from our intent's EXTRAS
    passedVar=getIntent().getStringExtra(Tutorial10.ID_EXTRA);
    //find out text view
    passedView=(TextView)findViewById(R.id.passed);
    //display our passed variable!!!
    passedView.setText("YOU CLICKED ON="+passedVar);
}

}

回答1:


You should change this line

  int id= ((int) listView.getItemIdAtPosition(i));

with this

  long id = adapterView.getItemIdAtPosition(i);

I hope this fixes your problem.




回答2:


According to the Android official document here, there's no definition about the purpose of this function. It seems to call to getItemId() in Adapter, and getItemId() returns the value of Id in SQLite.

I guess you need to use SimpleCursorAdapter to work with the SQLite.



来源:https://stackoverflow.com/questions/55447409/getitemidatposition-in-listview-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!