Faiz Mir Faiz Mir -4 years ago 100
Android Question

How to update sqlite on click on listview item

I have two activity. In first activity i have one listview and in second activity i have two editText and i am saving the data in sqlite through edittext and showing them in a listview i had done this things now i want when i click on listview item a activity will open with edittext which contain previous data so i can edit it and update the sqlite

Main.java

public class MainActivity extends AppCompatActivity {
List<FileName> filenames;
DBhelper dBhelper;
SQLiteDatabase sqLiteDatabase;
ListViewAdapter listViewAdapter;
ListView listView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent intent = new Intent(MainActivity.this, Note.class);
startActivity(intent);
return true;
case R.id.action_credits:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.lv_filename);
dBhelper = new DBhelper(getApplicationContext());
sqLiteDatabase =dBhelper.getReadableDatabase();
filenames = dBhelper.getItemFromDatabase(sqLiteDatabase);
if (filenames != null) {
listViewAdapter = new ListViewAdapter(getApplicationContext(), R.layout.listview_item, filenames);
listView.setAdapter(listViewAdapter);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}

}


Note.java

public class Note extends AppCompatActivity {
EditText editText;
EditText editText2;
DBhelper dBhelper;
SQLiteDatabase sqLiteDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
editText = (EditText)findViewById(R.id.et_title);
editText2 = (EditText)findViewById(R.id.et_note);


}


public void SaveNote()
{
dBhelper = new DBhelper(this);
sqLiteDatabase= dBhelper.getWritableDatabase();
dBhelper.store(editText.getText().toString(),editText2.getText().toString(),sqLiteDatabase);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_note, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_save:
SaveNote();
Intent intent = new Intent(Note.this, MainActivity.class);
startActivity(intent);
default:
return super.onOptionsItemSelected(item);
}

}
}


DBhelper.java

public class DBhelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "MEMBERSDATA.DB";

DBhelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE "+Coloumn.NewInfo.TABLE_NAME+"("+ Coloumn.NewInfo.TITLE+" TEXT,"+ Coloumn.NewInfo.NOTE+" TEXT);");

}
public void store(String Title,String Note, SQLiteDatabase sqLiteDatabase2) {
sqLiteDatabase2.execSQL("insert into " + Coloumn.NewInfo.TABLE_NAME + " (" + Coloumn.NewInfo.TITLE + ", " + Coloumn.NewInfo.NOTE+ ") values(?,?);", new String[]{Title,Note});

}
public List<FileName> getItemFromDatabase(SQLiteDatabase sqLiteDatabase) {
List<FileName> result = new ArrayList<>();
Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM "+Coloumn.NewInfo.TABLE_NAME,null);
while (c.moveToNext()) {
result.add(
new FileName(
c.getString(c.getColumnIndex("Name")),
c.getString(c.getColumnIndex("Note"))
)
);
}
c.close();
return result;
}





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

}
}

Answer Source

In your OnitemClickListener get Data from your database for that item and send them to your second activity and set those values to edit text like that

  String name = filenames.get(positiom).getName(); //get these values based on your FileName class
    String note = filenames.get(positiom).getNote();//get these values based on your FileName class
    Intent intent =  new Intent(MainActivity.this,Note.class);
    intent.putExtra("Name",name);
    intent.putExtra("Note",note);
    startActivity(intent);

and in your Note Activity oncreate method set these values to edit text

  String name = getIntent().getStringExtra("Name");
    String note = getIntent().getStringExtra("Note");
     editText1.setText(name);
     editText2.setText(note);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download