kashifmehmood kashifmehmood - 4 years ago 172
Android Question

Populating a textview of a class with data from another class

I have developed some sms application and I can't figure out how to solve this issue... in one layout I am displaying two textviews to display the number and the date of the sms... that works fine...

But now I am implementing the onListItemClick and I want that when list item is clicked the whole messages from the database should load into the new view.... How can I accomplish that...

protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);


Cursor c=dba.executeQuery("select rowid _id,* from messages where rowid="+position+";");
Log.v(tag, "value count"+ c.getCount());
Log.v(tag, "value count"+c.getCount());
Intent intent = new Intent();

String mesage = c.getString(c.getColumnIndex("msg"));

Log.v("Value passed to class", mesage);
intent.putExtra(name, mesage);

intent.setClass(this, NewInterface.class);



And here is the code of the other class to which I am passing data.

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
tv4 = (TextView) findViewById(R.id.tvMessages);
Bundle bundle=getIntent().getExtras();
Log.v("Value got from class", bundle.getString("name"));

The list view is populated via this method from database containing the phone numbers....

private void openAndQueryDatabase() {

newDB=new DBAdapter(this);

try {

Cursor c = newDB.executeQuery("select distinct nm,rowid _id from messages");

if (c != null ) {

if (c.moveToFirst())
Log.v(tag, "Stage 1");{
do {

String firstName = c.getString(c.getColumnIndex("nm"));
Log.v(tag, firstName);
// String date = c.getString(c.getColumnIndex("dt"));


}while (c.moveToNext());
Log.v(tag, "Stage 1");

Answer Source

On click of List item you are opening a db to fetch data based on position clicked. Instead of trying to read the whole datain your first Activity, why don't you just pass the int position to the next Activity using Intent and then make the database query there. It would be easier and make more sense. If you still want to pass the wholde data , use a code similar to the one below (replacing your if(c.getCount>0) block):

 ArrayList<String> arrayList ;

            Cursor c=null;
                int x= c.getCount();
                int  y =c.getColumnIndex("msg");
            arrayList = new ArrayList<String>(x);
                for(int a=0;a<x;a++){

                  Intent intent = new Intent(this,NewInterface.class);
                intent.putStringArrayListExtra("msgs",arrayList );

To get the number from one of the textViews of your clicked row of ListView:

TextView tv1=(TextView)v.findViewbyId(R.id.number)//Replace this with id of tv displaying numbers
String number = tv1.getText().toString();

Add these lines in your onListItemClick and then use this number in your query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download