0
Guys, I’m having a hard time with something. I created a list that shows contact names, but these contacts have more attributes, such as date and number. I made another screen for when the person click on the list, go on this other screen and shows all the details of such contact. I did such encoding. (I am using Sqlite to save/use contacts) if this is relevant On the contact details screen, I did this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact__detail);
long idSelected = getIntent().getLongExtra("ID", 0);
long positionSelected = getIntent().getIntExtra("POSITION", 0);
textView_dName = (TextView) findViewById(R.id.textView_dName);
textView_dDate = (TextView) findViewById(R.id.textView_dDate);
textView_dNumber = (TextView) findViewById(R.id.textView_dNumber);
}
My question is: How do I get textviews to receive past values? For example: textView_dName.setText such a. I ask for help please with such code, thanks in advance!
EDITED
Class Dbhelper:
public final static String DATABASE = "mydb";
public final static String TABLE = "contacts";
public final static String NAME = "name";
public final static String DATE = "date";
public final static String NUMBER = "numberr";
public DBHelper(Context context) {
super(context, DATABASE, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists "+TABLE+" (id integer primary key, "+NAME+" text, "+DATE+" text, "+NUMBER+" text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean insertContact(String name, String date, String number){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(NAME, name);
content.put(DATE, date);
content.put(NUMBER, number);
db.insert(TABLE, null, content);
return true;
}
public ArrayList<Contact> getAllContacts(){
ArrayList<Contact> myArray = new ArrayList<Contact>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("select * from "+TABLE, null);
cur.moveToFirst();
while(cur.isAfterLast()==false){
Contact c = new Contact();
c.setName(cur.getString(cur.getColumnIndex(NAME)));
c.setDate(cur.getString(cur.getColumnIndex(DATE)));
c.setNumber(cur.getString(cur.getColumnIndex(NUMBER)));
myArray.add(c);
cur.moveToNext();
}
return myArray;
}
//
public Cursor getAllContactss(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur1 = db.rawQuery("select * from "+TABLE, null);
return cur1;
}
Mainactivity:
DBHelper myDb = new DBHelper(this);
Cursor cursor = myDb.getAllContactss();
String[] columns = new String[]{
DBHelper.NAME,
DBHelper.DATE,
DBHelper.NUMBER
};
int[] to = new int[]{
R.id.textView_l1,
R.id.textView_l2,
R.id.textView_l3
};
ListView listView_contacts = (ListView) findViewById(R.id.listView_contacts);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleCursorAdapter myAdapter1 = new SimpleCursorAdapter(this,
R.layout.layoutdalist,
cursor,
columns,
to,
0);
listView_contacts.setAdapter(myAdapter1);
listView_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor1 = (Cursor) listView_contacts.getItemAtPosition(position);
String nome = cursor1.getString(cursor1.getColumnIndex(DBHelper.NAME));
String data = cursor1.getString(cursor1.getColumnIndex(DBHelper.DATE));
String number = cursor1.getString(cursor1.getColumnIndex(DBHelper.NUMBER));
Intent intent = new Intent(getApplicationContext(), Contact_Detail_Activity.class);
intent.putExtra("NOME", nome);
intent.putExtra("DATA", data);
intent.putExtra("NUMERO", number);
startActivity(intent);
/* Intent intent = new Intent(getApplicationContext(), Contact_Detail_Activity.class);
intent.putExtra("ID", id);
intent.putExtra("POSITION", position);
startActivity(intent); */
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_item_new_contact:
startActivity(new Intent(MainActivity.this, NewContactActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
protected void onResume() { super.onResume();
DBHelper myDb = new DBHelper(this);
ArrayList<String> arrayNames = new ArrayList<String>();
for(int i = 0; i < myDb.getAllContacts().size(); i++){
arrayNames.add(myDb.getAllContacts().get(i).getName());
}
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayNames);
listView_contacts.setAdapter(myAdapter);
Your doubt would be in relation to
TextView
receive the values or how to make you search for this data in Sqlite?– Vitor Henrique
My question is how would I set the data that I have stored in the text views of the other Activity. I hope you understand, I think it would be in relation to Textview itself
– Viskee
But in your question is already the answer, just use the function you said:
textView_dName.setText("texto recebido");
. The steps would be to take your variableidSelected
that is coming in Intent, consult the bank contact with this identification and pass the data through thesetText
ofTextView
, that would be it?– Vitor Henrique
Hmm, could you explain to me how I would consult at the bank using the
idSelected
? and give thesetText
with the result?– Viskee
The id that shows on Adapter is not the id in the database, you need to create an Adapter that stores this ID or do a Hashmap to connect the X ID to the item ID in the adapter.
– Lucas Queiroz Ribeiro
If possible, show how you populate this Listview, then it is easier to suggest how to implement this issue
– Lucas Queiroz Ribeiro
Another option is to use a Simplecursoradapter, in which case the ID in the item will be the _id of the table.
– Lucas Queiroz Ribeiro
@Lucasqueirozribeiro I edited my post and put more information about the program. I put the class
DBhelper
which has a population method and theMainActivity
which has a repopulation method as well. Thanks for the help– Viskee