samedi 6 février 2016

EditText SET TEXt on listview ITEMCLICKLISTENER from SQlite database

To All Masters out there i need some help..i am a beginner..i made a lot of research and look for other codes that might solved this problem but it all did not work.. I have an Activity that save all my inputs and retrieve all those inputs on another activity..now in my listview i have rows that is set to horizontal layout..if i click a row i want my EditText to display the certain row name i need aquery method in my helper class and how to implement it on my listview onitemclicklistener..thank you..


THIS IS MY ACTIVITY THAT SHOW ALL THE INPUTS FROM MY OTHER ACTIVITY


public class ViewListsActivity extends AppCompatActivity {
DatabaseHelper databaseHelper=new DatabaseHelper(this);
SQLiteDatabase sqLiteDatabase ;
ListDataAdapter listDataAdapter ;
ListView listView;
Cursor cursor ;
EditText delete_txtview = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.view_lists_activity);
        listView = (ListView) findViewById(R.id.search_listView);
        delete_txtview = (EditText) findViewById(R.id.delete_text);
    databaseHelper=new DatabaseHelper(this);
    listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
    databaseHelper = new DatabaseHelper(getApplicationContext());
        sqLiteDatabase = databaseHelper.getReadableDatabase();
        cursor = databaseHelper.getInformations(sqLiteDatabase);
        listView.setAdapter(listDataAdapter);
    if (cursor.moveToFirst()) {
            do {
                String description, category, month1, remind, qty, location;
                description = cursor.getString(0);
                category = cursor.getString(1);
                month1 = cursor.getString(2);
                qty = cursor.getString(3);
                remind = cursor.getString(4);
                location = cursor.getString(5);
                DataProvider dataProvider = new DataProvider(description, category, month1, qty, remind, location);
                listDataAdapter.add(dataProvider);

            } while (cursor.moveToNext());

        }

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        }
    });

}

public void ToSearchBtn(View view) {
    Intent intent = new Intent(this, ThirdActivitySearchAllData.class);
    startActivity(intent);
}
public void ToAddNewItemBtn(View view) {
    Intent intent = new Intent(this, SecondActivitySaveData.class);
    startActivity(intent);
}
}

THIS IS MY HELPER CLASS


public class DatabaseHelper extends SQLiteOpenHelper{
final static String  ITEM_ID = "myid";
final static String  LOCATION = "location";
final static String  DESCRIPTION = "description";
final static String  CATEGORY = "category";
final static String  MONTHONE ="monthOne";
final static String  REMIND_AT ="remindAt";
final static String  QTY ="quantity";
final static String  TABLE_NAME ="add_new";
final static String  DATABASE_NAME=" EXPIRATIONMONITORING.DB";
final static  int DATABASE_VERSION = 1;
String CREATE_QUERY =
        "CREATE TABLE "+TABLE_NAME+" ("+ITEM_ID+"  INTEGER PRIMARY KEY  AUTOINCREMENT, "+DESCRIPTION+" TEXT,  "+CATEGORY+" TEXT, "+MONTHONE+" TEXT, "+REMIND_AT+" TEXT, "+QTY+" TEXT, "+LOCATION+" TEXT )";
SQLiteDatabase sqLiteDatabase;
//DatabaseHelper databaseHelper;
public DatabaseHelper(Context context)
{

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    //this.context1=context;
    Log.e("DATABASE OPERATIONS", "Database created / opened....");
}
@Override
public void onCreate(SQLiteDatabase db) {


    db.execSQL(CREATE_QUERY);

    Log.e("DATABASE OPERATIONS", "Table created....");

}
public void openDB() throws SQLiteException {
    sqLiteDatabase = this.getWritableDatabase();
}
public void closeDB(){
    this.close();
}

public void addInformations(String description, String category, String monthOne,String quantity,String remind, String location, SQLiteDatabase db)
{
    ContentValues contentValues=new ContentValues();
    contentValues.put(LOCATION,location);
    contentValues.put(DESCRIPTION,description);
    contentValues.put(CATEGORY,category);
    contentValues.put(MONTHONE,monthOne);
    contentValues.put(REMIND_AT,remind);
    contentValues.put(QTY,quantity);
    db.insert(TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATIONS", "One row inserted");
    db.close();
}

public Cursor getInformations(SQLiteDatabase sqLiteDatabase)
{
    Cursor cursor;
    String[] projections ={DESCRIPTION,CATEGORY,MONTHONE, QTY, REMIND_AT,LOCATION};
    cursor=sqLiteDatabase.query(TABLE_NAME, projections, null, null, null, null, null);
    return cursor;
}
public Cursor getContact(String location,SQLiteDatabase sqLiteDatabase)
{
    String[] projections ={DESCRIPTION,CATEGORY,MONTHONE,QTY, REMIND_AT,LOCATION};
    String selection = LOCATION+" LIKE? ";
    String [] sargs={location};
    Cursor cursor=sqLiteDatabase.query(TABLE_NAME,projections,selection,sargs,null,null,null);
    return  cursor;
}

//For AutoCompleteTextView searching
public String[] SelectAllData()
{
    try
    {
        String arrData[]=null;
        SQLiteDatabase db;
        db=this.getReadableDatabase();
        String strSQL=" SELECT "+LOCATION+" FROM "+TABLE_NAME;
        Cursor cursor =db.rawQuery(strSQL,null);
        if(cursor !=null)
        {
            if(cursor.moveToFirst())
            {
                arrData=new String[cursor.getCount()];
                int i=0;
                do
                {
                    arrData[i]=cursor.getString(0);
                    i++;

                }while(cursor.moveToNext());
            }
        }
        cursor.close();
        return arrData;

    }catch(Exception e){
        return null;
    }
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

this is my LIST ADAPTER

public class ListDataAdapter extends ArrayAdapter  {
List list = new ArrayList();

public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}



static class LayoutHandler
{
    TextView DESCRIPTION,CATEGORY,MONTH1,QTY,REMIND,LOCATION;
}
public void add(Object object)
{
    super.add(object);
    list.add(object);
}

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

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

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

    View row =convertView;
    LayoutHandler layoutHandler;

    if(row==null)
    {
        LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler=new LayoutHandler();

        layoutHandler.DESCRIPTION= (TextView) row.findViewById(R.id.text_description);
        layoutHandler.CATEGORY= (TextView) row.findViewById(R.id.text_category);
        layoutHandler.MONTH1= (TextView) row.findViewById(R.id.text_monthOne);
        layoutHandler.REMIND= (TextView) row.findViewById(R.id.text_remind);
        layoutHandler.QTY= (TextView) row.findViewById(R.id.text_qty);
        layoutHandler.LOCATION= (TextView) row.findViewById(R.id.text_location);
        row.setTag(layoutHandler);
    }else
    {
        layoutHandler=(LayoutHandler)row.getTag();

    }
    DataProvider dataProvider= (DataProvider) this.getItem(position);
    layoutHandler.DESCRIPTION.setText(dataProvider.getDescription());
    layoutHandler.CATEGORY.setText(dataProvider.getCategory());
    layoutHandler.MONTH1.setText(dataProvider.getMonthOne());
    layoutHandler.REMIND.setText(dataProvider.getRemindAt());
    layoutHandler.QTY.setText(dataProvider.getQuantity());
     layoutHandler.LOCATION.setText(dataProvider.getLocation());


    return row;
}


}


Aucun commentaire:

Enregistrer un commentaire