如何在listview项中的每个项中获取ID来自数据库

时间:2018-04-17 13:52:32

标签: android listview android-cursor

这是我活动中的代码

 @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;
    }

}

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

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

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

答案 1 :(得分:0)

首先,获取listDataAdapter以按位置返回DataProvider,然后像这样得到它的Id:

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

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

    }
});