0

I created an adapter for my ListView that's displaying a data from my SQLite database, the problem is that in the ListView, I have 5 times the same line: My adapter's code:

public class MyAdapter extends ArrayAdapter<Historique> {
    private LayoutInflater mInflat;
    private ArrayList<Historique> hist = new ArrayList<Historique>();
    private int mVRessId;


    public MyAdapter (Context context, int ressId, ArrayList<Historique> hists){

        super(context,ressId,hists);
        this.hist =hists;
        mInflat = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mVRessId =ressId;
    }

    public View getView(int position, View convertedView, ViewGroup parents){
        convertedView = mInflat.inflate(mVRessId,null);

        Historique histor = hist.get(position);



        if (histor != null){
            TextView name = (TextView) convertedView.findViewById(R.id.hnom);
            TextView quest = (TextView) convertedView.findViewById(R.id.hques);
            TextView rep = (TextView) convertedView.findViewById(R.id.hrep);
            TextView date = (TextView) convertedView.findViewById(R.id.hdate);

            if (name != null){
                name.setText(""+histor.getNom()+ ": "+histor.getLigne());
            }
            if (quest != null){
                quest.setText(histor.getQuest());
            }
            if (rep != null){
                rep.setText(histor.getRep());
            }
            if (date != null){
                date.setText(histor.getDate().toString());
            }
        }
        return convertedView;
    }
}

The ListView must display the list, and here's the code:

final MyAdapter adapter =  new MyAdapter (this, R.layout.adapter_view_layout,histList);





mCsr = openhelper.getTableHistoAsCursor();
int rows = mCsr.getCount();
if (rows == 0 ){
    Toast.makeText(Historian.this, "Pas d'historique disponible", Toast.LENGTH_SHORT).show();
} else {
    while (mCsr.moveToNext()){

        histo = new Historique(mCsr.getString(0).toString(),mCsr.getString(1).toString(), mCsr.getString(2).toString(),mCsr.getString(3).toString(),mCsr.getString(4).toString());
        histList.add(histo);

   }


}
mListView .setAdapter(adapter);

The getTableHisto is a function that returns a cursor from my SQLite Modelhelper:

public Cursor getTableHistoAsCursor() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor data = db.rawQuery(" SELECT Q." + KEY_QUESTION + " ,L." + KEY_LIGNE + " , U." + KEY_NOM + " , A." + KEY_DATE + " , A. " + KEY_REPONSE + " from "+ TABLE_LIGNE + " L, " + TABLE_QUESTION + " Q, " + TABLE_USER + " U, " + TABLE_ANSWER + " A WHERE Q." + KEY_ID_QUESTION + " = A." + KEY_ID_QUESTION + " AND A." + KEY_MATRICULE + " = U." + KEY_MATRICULE, null);
        return data;
    }

In the first time, I thought that in the insert I was doing something wrong and it goes 5 times in the database, so I've created an activity to try to get the number of inserts with a Select count(); And I saw that the result that I'm getting is the true result, I don't insert 5 times the same line, but the adapter is displaying it 5 times in 5 lines. If you have any idea that can help me, I would be thankful. thanks.

Master49
  • 61
  • 7

2 Answers2

0

I got it, the problem is solved, in case someone has the same problem, the loop wasn't good so I changed

else {
    while (mCsr.moveToNext()){

by

else {
     if (mCsr != null){
        mCsr.moveToFirst();
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Master49
  • 61
  • 7
0
//Initialize my adapter after populating the list

histList.add(histo);

Adapter is only initialized after populating values in the Array List not with null values.

final MyAdapter adapter =  new MyAdapter (this, R.layout.adapter_view_layout,histList);    
mListView .setAdapter(adapter);
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
gorp88
  • 105
  • 14