如何从数据库中获取列表视图项目中每个项目的ID

How to get ID in each item in the listview item is from the database

这是我在 activity

中的代码
 @Override
        protected void onCreate( Bundle savedInstanceState ) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.patient_list);

            try{

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                displaypatientinfo = (ListView)findViewById(R.id.lv_patientlist);
                mydb  = new DatabaseHelper(getApplicationContext());
                sqLiteDatabase = mydb.getReadableDatabase();
                String selection = "pat_type=? OR pat_type=? AND is_active=?";
                String[] selectionargs = {"Infant","Mother","1"};
                cursor  = mydb.getallpatient(sqLiteDatabase,selection,selectionargs);
                listDataAdapter = new ListDataAdapter(this, R.layout.resource_patientlist);
                displaypatientinfo.setAdapter(listDataAdapter);
                if(cursor.moveToFirst()){
                    do{
                        String id, firstname, middlename, lastname, birthdate;
                        id = cursor.getString(0);`enter code here`
                        firstname = cursor.getString(1);
                        middlename = cursor.getString(2);
                        lastname = cursor.getString(3);
                        birthdate = cursor.getString(4);
                        DataProvider dataProvider = new DataProvider(id, firstname, middlename, lastname, birthdate);
                        listDataAdapter.add(dataProvider);
                    }while(cursor.moveToNext());
                }else{


                }

       displaypatientinfo.setOnItemClickListener(new OnItemClickListener() {
                    @Override
 public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
                       // just wanna ask your help here that if the user click item on listview it will return the id from the database 



                    }
                });
            }catch (Exception e){
                e.printStackTrace();
            }

        }

这是我的列表适配器中的代码

package com.example.asus.wombapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.asus.wombapp.onclicklistener.PatientList_Listener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by asus on 11/04/2018.
 */

public class ListDataAdapter extends ArrayAdapter {

    private Context context;

    List list = new ArrayList();

    public ListDataAdapter( Context context, int resource) {

        super(context, resource);
        this.context = context;
    }




    static class LayoutHandler
    {
        TextView id, name, birthdate;
        ImageView patientimage,edit,delete;

    }

    @Override
    public void add(Object object){
        super.add(object);
        list.add(object);
    }


    @Override
    public int getCount(){
        return list.size();
    }

    @Override
    public Object getItem(int position){

        return list.get(position);
    }


    @Override
    public View getView( int position, View ConvertView, ViewGroup parent){

        ImageView delete;
        View row = ConvertView;
        LayoutHandler layoutHandler;
        DataProvider dataprovider = (DataProvider) this.getItem(position);
        if(row  == null){
            LayoutInflater layoutInflater= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            row  = layoutInflater.inflate(R.layout.resource_patientlist,parent,false);
            layoutHandler = new LayoutHandler();
            final PatientList patientList = new PatientList();
            layoutHandler.id = (TextView)row.findViewById(R.id.tv_id);
            layoutHandler.name = (TextView)row.findViewById(R.id.tv_patientname);
            layoutHandler.birthdate = (TextView)row.findViewById(R.id.tv_actualbirthdate);
            layoutHandler.patientimage = (ImageView) row.findViewById(R.id.image_patient);
            layoutHandler.edit = (ImageView)row.findViewById(R.id.image_edit);
            layoutHandler.delete = (ImageView)row.findViewById(R.id.image_delete);
            layoutHandler.delete.setOnClickListener(new PatientList_Listener(this.context,dataprovider.getId()));
            layoutHandler.edit.setOnClickListener(new PatientList_Listener(this.context,dataprovider.getId()));
            row.setTag(layoutHandler);
        }else{
            layoutHandler = (LayoutHandler)row.getTag();
        }
        //set value from database to resourceview

        layoutHandler.id.setText(dataprovider.getId());
        layoutHandler.name.setText(dataprovider.getFirtsname() + " " + dataprovider.getMiddlename() +" " + dataprovider.getLastname());
        layoutHandler.birthdate.setText(dataprovider.getBirthdate());
        layoutHandler.patientimage.setImageResource(R.drawable.picture);
        layoutHandler.delete.setImageResource(R.drawable.delete_data);
        layoutHandler.edit.setImageResource(R.drawable.edit_data);

        return row;
    }

}

希望你能帮助我 activity 我有一个列表视图,其中所有数据都来自数据库 我只是想请你帮忙如何获取列表视图中项目的 ID。一旦用户单击列表视图上的项目,我就可以获得该项目的主键,请帮助我我试图获取的 ID 是 listAdapter 中的 dataprovider.getId() 请希望任何人都可以提供帮助

您可以使用以下内容:

((DataProvider)listDataAdapter.getItem(position)).getId();

首先,将 listDataAdapter 按位置获取到 return DataProvider,然后像这样获取它的 Id:

displaypatientinfo.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {

        String id = listDataAdapter.get(position).getId();

    }
});