vendredi 1 mai 2015

how to display data of Sqlite on two separate Textview

i'm trying develop an app which save points and date into two separate TextView into ListView. when execute de app i save data in Sqlite but in the ListView, in the TextView of points always show '2'. why? my code:

public class BaseDeDatos extends SQLiteOpenHelper{


public BaseDeDatos(Context context) {
    super(context, "puntuaciones", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE puntuaciones (puntos INTEGER,fecha VARCHAR)");

}
public void savePoints(int points,String date){
     SQLiteDatabase db = getWritableDatabase();
     db.execSQL("INSERT INTO puntuaciones VALUES("+points+","+"'"+date+"')");
     db.close();
}
public Vector<String> listaPuntos(int cantidad) {
    Vector<String> result = new Vector<String>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT puntos, fecha FROM " +
    "puntuaciones ORDER BY puntos DESC LIMIT " +cantidad, null);
    while (cursor.moveToNext()){

    result.add(cursor.getInt(0)+cursor.getString(1));
    }
    cursor.close();
    db.close();
    return result;
    }


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

the adapter:

public class TodoAdapter extends BaseAdapter{

private Vector<String> todoList;
//private LayoutInflater inflater;
Context contexto; 
public TodoAdapter(Context context, Vector<String> todoList){
    this.todoList = todoList;
    contexto=context;
}

@Override
public int getCount() {
    return todoList.size();
}
@Override
public Object getItem(int position) {

    return todoList.elementAt(position);
}


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

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HolderA holder;
    if(convertView == null){
        LayoutInflater inflater = LayoutInflater.from(contexto);
        convertView = inflater.inflate(R.layout.elemento_lista, null);
        holder= new HolderA();
        holder.puntos= (TextView) convertView.findViewById(R.id.tv1);
        holder.fecha = (TextView) convertView.findViewById(R.id.tv2);
        convertView.setTag(holder);
    }else{
        holder=(HolderA) convertView.getTag();
    }


    holder.puntos.setText(String.valueOf(getItem(position).toString().length()-19));
    holder.date.setText(getItem(position).toString().substring(getItem(position).toString().length()-19, getItem(position).toString().length()));
    return convertView;
}

static class HolderA{

    TextView puntos;
    TextView date;
    }

and finally the method which save date

public String makeDate(){
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    String date=dateFormat.format(cal.getTime());
    return date;
}

Aucun commentaire:

Enregistrer un commentaire