samedi 19 septembre 2015

onClick display information of item I've just clicked

I'm trying to make an application in which user can add an information: Name, date of birth, time of birth. In listView it should display only his name and date of birth. I figured out how to do it. Right now I have 2 problems. First - it doesn't open my first layout when I press 2nd button. Second - In second layout it should display how old he is like that "DD/MM/YY HH:MM:SS" and update in real time. I just can't understand how to take on item click information of this very item. DB Code

 private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1500;
private static final String DB_TABLE = "mytab";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMG = "img";
public static final String COLUMN_TXT = "txt";
public static final String COLUMN_NMB = "nmb";

private static final String DB_CREATE =
        "create table " + DB_TABLE + "(" +
                COLUMN_ID + " integer primary key autoincrement, " +
                COLUMN_IMG + " integer, " +
                COLUMN_TXT + " text, " +
                COLUMN_NMB + " number" +
                ");";

private final Context mCtx;


private DBHelper mDBHelper;
private SQLiteDatabase mDB1;

public DB2(Context ctx) {
    mCtx = ctx;
}

public void open() {
    mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
    mDB1 = mDBHelper.getWritableDatabase();
}

public void close() {
    if (mDBHelper!=null) mDBHelper.close();
}


public Cursor getAllData() {
    String[] columns = {"_id","txt","img","nmb"};
    return mDB1.query(DB_TABLE, columns, null, null, null, null, null);
}

public void addRec(String txt, int img, int nmb) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TXT, txt);
    cv.put(COLUMN_IMG, img);
    cv.put(COLUMN_NMB, nmb);
    mDB1.insert(DB_TABLE, null, cv);
}

public void delRec(long id) {
    mDB1.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
}

private class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                    int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

Activity code

 private static final int CM_DELETE_ID = 1;
int nmb1;
ListView lvData;
DB2 db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
EditText et,nmb;
Button bt,bt2;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test4);
    et =(EditText)findViewById(R.id.editText2);
    nmb = (EditText)findViewById(R.id.nmb);
    bt = (Button)findViewById(R.id.button);
    //bt.setOnClickListener(this);
    bt2 = (Button)findViewById(R.id.btn2);
    //bt2.setOnClickListener(this);
    db = new DB2(this);
    db.open();



    cursor = db.getAllData();
    startManagingCursor(cursor);

    String[] from = new String[] { DB2.COLUMN_IMG, DB2.COLUMN_TXT, DB2.COLUMN_NMB };
    int[] to = new int[] { R.id.ivImg, R.id.tvText, R.id.tvNmb };

    scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
    lvData = (ListView) findViewById(R.id.lvData);
    lvData.setAdapter(scAdapter);

    registerForContextMenu(lvData);

    lvData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView parentView, View childView,
                                   int position, long id) {
            setContentView(R.layout.item_info);
        }

        public void onNothingSelected(AdapterView parentView) {

        }
    });


}

private void buttonClick()
{
    setContentView(R.layout.item_info);
}
private void button3Click() {
    nmb1 = Integer.parseInt(nmb.getText().toString());
    db.addRec(et.getText().toString(), R.mipmap.ic_launcher, nmb1);
    cursor.requery();;
}


public void onButtonClick(View view) {
    switch(view.getId())
    {
        case R.id.button: {
            buttonClick();
            break;
        }
        case R.id.btn2: {
            setContentView(R.layout.activity_test4);
            button3Click();
            break;
        }

    }




}

public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
}




public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == CM_DELETE_ID) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        db.delRec(acmi.id);
        cursor.requery();
        return true;
    }
    return super.onContextItemSelected(item);
}

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

If I try "implements View.OnClickListener" it just crashes

Aucun commentaire:

Enregistrer un commentaire