如果行ID与listView项ID匹配,则显示SQLite行值

时间:2016-05-02 13:09:20

标签: android sqlite listview

这是我的情景:

假设我有一个SQLite表,它显示了一些行和ID当然是自动递增的。

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


                // get the IDs of each column in the database
                String [] IDs = activity.getAllColumnsIDs().toArray(
                        new String[activity.getAllColumnsIDs().size()]);

                // loop throught the IDs and see if they match the listView
                // item ids. if yes, display the detail
                DataBaseHelper summerJobDB;
                summerJobDB = new DataBaseHelper(activity);
                for (int i = 0; i >= IDs.length; i++) {
                    if (i == id) {


                        Cursor res = summerJobDB.getAllData();
                        if (res.getCount() ==0){
                            // show some message
                            showMessage("Error", "Nothing found");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        // append the values from the database to the buffer.
                        // this is based on the index number of the columns
                        while (res.moveToNext()) {
                            buffer.append(res.getString(1)+ "/" + res.getString(2) +"\n");
                            buffer.append("Starts on " + res.getString(3)+"\n\n");
                            buffer.append("Address: \n"+ res.getString(5)+"\n\n");
                            buffer.append("Phone: \n"+ res.getString(6)+"\n\n");
                            buffer.append("Hours: \n"+ res.getString(4)+"\n\n");
                        }
                        // show all data here

                        showMessage("Data",buffer.toString());

                    }
                }

            }

&#34; placeName&#34;和&#34;位置&#34;和&#34; startingTime&#34;用于填充listView单元格,如下所示: Click here to see screen capture

现在我的问题是,如何显示表行的值,其ID与列表项的ID相匹配?因此,当用户按下某个项目时,将显示共享相同ID的表格行。感谢

这是我到目前为止所尝试的内容(仅限onItemClickedListner方法),但我知道我没有正确检索数据

public class HorariosAdapter extends RecyclerView.Adapter<HorariosAdapter.HorarioViewHolder> {
public static class HorarioViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView horaDesde;

    HorarioViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cvHorario);
        horaDesde = (TextView) itemView.findViewById(R.id.txtHora);
    }
}

List<Horario> horarios;

HorariosAdapter(List<Horario> pHorarios){
    this.horarios = pHorarios;
}

@Override
public int getItemCount() {
    return horarios.size();
}

@Override
public HorarioViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rvhorarios, viewGroup, false);
    HorarioViewHolder hvh = new HorarioViewHolder(v);
    return hvh;
}

@Override
public void onBindViewHolder(HorarioViewHolder horarioViewHolder, int i) {
    horarioViewHolder.horaDesde.setText(horarios.get(i).getHoraDesde());
}

0 个答案:

没有答案