Question

I've asked before about populating the list view with data from the database. Now I'm trying to make the items clickable and direct it to another class with some information that will be used for the update class.

I think I've managed to direct it to 'another' class by using intents but the problems is the data that I put on the intent doesn't transfer on the 'another' class.

here is the code: MySQLView.java

     ArrayList<String> data =  info.geData(); 
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));

lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
                   // TODO Auto-generated method stub

               /***I made the value of the item, numbers. cos I think its easier?***/


    String draftID = lv.getItemAtPosition(position).toString(); 
    Intent i =new Intent(SQLView.this, com.android.its.SQLiteExample.class);
          i.putExtra("draftID", draftID);
          startActivity(i);
/***just to check if it has value***/             
    Toast.makeText(getBaseContext(),"position is : "+position+" and value is "+
                   draftID,Toast.LENGTH_SHORT).show();


        }

     });

Then here is the SQLiteExample class which contains the editText

case R.id.bgetInfo:
        try {
            Bundle extras = getIntent().getExtras(); 
            String s= extras.getString("draftID");
            long l = Long.parseLong(s);
            HotOrNot hon = new HotOrNot(this);
            hon.open();
            String returnedName = hon.getName(l);
            String returnedHotness = hon.getHotness(l);
            hon.close();

            sqlName.setText(returnedName);
            sqlHotness.setText(returnedHotness);
        } catch (Exception e) {

            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Dang it!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }
        break;

Here's is my databasehelper class

 public ArrayList<String> geData() {
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    ArrayList<String> result = new ArrayList<String>();
    int iRow=c.getColumnIndex(KEY_ROWID);


    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        result.add(c.getString(iRow));
    }
    return result;
}
    if (c!=null){
        c.moveToFirst();

        String hotness=c.getString(2);
        return hotness;
    }
    return null;
}
public String getName(long l) throws SQLiteException{
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns,  KEY_ROWID+"="+l, null, null, null, null);
    if (c!=null){
        c.moveToFirst();

        String name=c.getString(1);
        return name;
    }
    return null;
}

public String getHotness(long l)throws SQLiteException {
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null); 

(here's my previous question How to get data from my database and put it on a ListView that is clickable? )

UPDATE : Okay,although my code is really messy, it seems that it was working all along, I didn't test the getInfo Button that's why it doesn't show up. Sorry for bothering you guys :p but I still want to know what will be your approach on putting a ROWID(database) in a listview and putting its value on other class ?

Was it helpful?

Solution

It should be. Your SqlLiteExample class which you launch via the Intent should retrieve the data you put in the Intent in the onCreate method

protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         Intent intent = getintent()
         String draftID = intent.getStringExtra("draftID");
     }

Are you doing this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top