First element in listView does not appear

3

I have a listView that lists through an adapter. The list is updated after a search in SQLite. The problem is that it only appears from the second element of my table .... could anyone help me?

Adapter:

public class ItemListHistoryAdapter extends BaseAdapter {

    private Context ctx;
    private List<ItemListHistory> listItem;

    public ItemListHistoryAdapter(Context context, List<ItemListHistory> listItem){

        this.ctx = context;
        this.listItem = listItem;

    }

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

    @Override
    public Object getItem(int position) {
        return listItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        //Layout que vai retornar
        View layoutReturn;

        if (convertView == null) {

            LayoutInflater inflaterHelper = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutReturn = inflaterHelper.inflate(R.layout.layout_list, null);


            convertView = inflaterHelper.inflate(R.layout.layout_list, null);
        }else {
            layoutReturn = convertView;
        }

        TextView tvScore = (TextView) layoutReturn.findViewById(R.id.ll_tv_score);
        if (Integer.parseInt(listItem.get(position).getTxtScore())>0){
            tvScore.setTextColor(ctx.getResources().getColor(R.color.deepskyblue));
        }else{
            tvScore.setTextColor(ctx.getResources().getColor(R.color.red));
        }
        tvScore.setText("Score: "+listItem.get(position).getTxtScore());

        TextView tvDate = (TextView) layoutReturn.findViewById(R.id.ll_tv_date);
        tvDate.setText(listItem.get(position).getTxtDate());

        ImageView ivCool = (ImageView) layoutReturn.findViewById(R.id.ll_iv_cool);
        ivCool.setImageResource(listItem.get(position).getIconId());

        return layoutReturn;
    }
}

Activity:

public class HistoryActivity extends Activity {

    private List<ItemListHistory> listItens;
    SQLiteOpenHelper dbHelper = new HistorySQLHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        //alterando a fonte do Titulo da Activity
        FontManager fontManager = new FontManager();
        TextView tvTitle = (TextView) findViewById(R.id.hi_tv_title);
        fontManager.changeFontTextView(this, tvTitle, fontManager.BLOODLUST_PATH);

        //meu banco de dados
        final SQLiteDatabase db = dbHelper.getWritableDatabase();

        //List View
        ListView lvHistory = (ListView) findViewById(R.id.hi_lv_list);

        listItens = findAllItens(db);

        //Adapter
        final ItemListHistoryAdapter listAdapter = new ItemListHistoryAdapter(this, listItens);
        lvHistory.setAdapter(listAdapter);

    }

    //retorna uma lista de itens do banco de dados
    private List<ItemListHistory> findAllItens(SQLiteDatabase db){
        int imageRId;

        db = dbHelper.getWritableDatabase();

        String sqlQuery = "SELECT * FROM "+HistorySQLHelper.TAB_HISTORY+" ORDER BY "+HistorySQLHelper.COLUMN_DATE+" DESC;";
        List<ItemListHistory> listItens = new ArrayList<ItemListHistory>();

        Cursor cursor = db.rawQuery(sqlQuery,null);
        cursor.moveToFirst();

        while (cursor.moveToNext()){
            String score = cursor.getString(cursor.getColumnIndex(HistorySQLHelper.COLUMN_SCORE));

            String date = cursor.getString(cursor.getColumnIndex(HistorySQLHelper.COLUMN_DATE));


            if (Integer.parseInt(score) < 0){
                imageRId = R.drawable.monster2;
            }else{
                imageRId = R.drawable.coin_gold;
            }

            ItemListHistory itemListHistory = new ItemListHistory(score,date,imageRId);

            listItens.add(itemListHistory);
        }
        cursor.close();
        db.close();

        return listItens;
    }

    public void clearRanking(View view){

        AlertDialog.Builder dialog = new AlertDialog.Builder(HistoryActivity.this);

        dialog.setMessage(R.string.msg_really);
        dialog.setTitle(R.string.msg_title_attention);
        dialog.setIcon(R.drawable.warning);
        dialog.setPositiveButton(R.string.bt_yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                SQLiteOpenHelper dbHelper = new HistorySQLHelper(getBaseContext());
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                String sql = "DELETE FROM " + HistorySQLHelper.TAB_HISTORY + "";
                db.execSQL(sql);
                db.close();
                Toast.makeText(getBaseContext(), "Ranking clear...", Toast.LENGTH_SHORT).show();
                finish();

            }
        });
        dialog.setNegativeButton(R.string.bt_no, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void closeActivity(View view){
        finish();
    }

}
    
asked by anonymous 02.07.2015 / 15:01

1 answer

7

Analyzing the following part of your code:

Cursor cursor = db.rawQuery(sqlQuery,null);
cursor.moveToFirst();

while (cursor.moveToNext()){

    .....
    .....
}

It turns out that cursor is positioned in the first register through cursor.moveToFirst(); .

Then, in while , the cursor is positioned in the next record, when cursor.moveToNext() is done, the first record being ignored.

Delete the line cursor.moveToFirst(); .

Edit after my comment

The most correct way will be:

Cursor cursor = db.rawQuery(sqlQuery,null);
if(cursor != null && cursor.moveToFirst()){
    do{
        .....
        .....
    }while(cursor.moveToNext());
}
    
02.07.2015 / 15:18