I am making a simple Android app that takes the name, email and contact number of a user and saves these (including an id) in the phone's memory (via sqlite). A "Show All" page displays the details of all the user in the database in a ListView using a custom adapter. It runs like this:
It leaves these spaces in between the rows. How do I remove these spaces? Also, the details get mixed up while retrieving from the database. Like the first row is displayed in the correct format (as I wanted) . But the second's details got mixed up. How do I correct this?
MyDBHandler.java
public class MyDBHandler extends SQLiteOpenHelper{
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME= "user.db";
    public static final String TABLE_NAME = "data";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_email = "email";
    public static final String COLUMN_phno = "phno";
    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE "+TABLE_NAME+"("+
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
                COLUMN_NAME + " VARCHAR(20),"+
                COLUMN_email + " VARCHAR(20),"+
                COLUMN_phno + " VARCHAR(20)"+
                ");";
        db.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public void addData(Data d){
        ContentValues values = new ContentValues();
        //values.put(COLUMN_ID, d.getId());
        values.put(COLUMN_NAME, d.getName());
        values.put(COLUMN_email, d.getEmail());
        values.put(COLUMN_phno, d.getPhno());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();
    }
    public ArrayList<String> retrieveData(){
        ArrayList<String> al = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM "+TABLE_NAME+";";
        Cursor c = db.rawQuery(query,null);
        c.moveToFirst();
        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("id"))!=null){
                al.add(c.getString(c.getColumnIndex("id")));
                al.add(c.getString(c.getColumnIndex("name")));
                al.add(c.getString(c.getColumnIndex("email")));
                al.add(c.getString(c.getColumnIndex("phno")));
            }
            c.moveToNext();
        }
        db.close();
        return al;
    }
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
    ArrayList<String>a = new ArrayList<>();
    public CustomAdapter(Context context,   ArrayList<String>a ){
        super(context,R.layout.custom_row,a);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater harshitsInflator = LayoutInflater.from(getContext());
        View customView = harshitsInflator.inflate(R.layout.custom_row, parent, false);
        TextView textView3 = (TextView) customView.findViewById(R.id.textView3);
        TextView textView4 = (TextView) customView.findViewById(R.id.textView4);
        TextView textView5 = (TextView) customView.findViewById(R.id.textView5);
        Button button4 = (Button) customView.findViewById(R.id.button4);
        String Sid = getItem(position);
        if(position%4==0 && position>0)
        {
            textView3.setText(a.get(1));
            textView4.setText(a.get(2));
            textView5.setText(a.get(3));
            button4.setText(a.get(0));
            a.clear();
            customView.setVisibility(View.VISIBLE);
        }
        else {
            a.add(Sid);
            customView.setVisibility(View.GONE);
        }
        return customView;
    }
}
listView.java
public class listView extends Activity {
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_layout);
        ArrayList<String> n;
        MyDBHandler db = new MyDBHandler(this, null, null, 1);
        n=db.retrieveData();
        ListAdapter harshitsAdapter = new CustomAdapter(this, n);
        ListView harshitsListView = (ListView) findViewById(R.id.harshitsListView);
        harshitsListView.setAdapter(harshitsAdapter);
        //((BaseAdapter)harshitsAdapter).notifyDataSetChanged();
    }
    public void backToMain(View view){
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
        finish();
    }
}
This app also shows ArrayIndexOutOfBounds Exception:
Process: com.example.h8pathak.dilliheart, PID: 21258
    java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
How do I also rectify this one?
 
Aucun commentaire:
Enregistrer un commentaire