1

以前、リスト ビューにデータベースのデータを取り込むことについて質問したことがあります。現在、項目をクリック可能にして、更新クラスに使用される情報を別のクラスに送信しようとしています。

インテントを使用して「別の」クラスに送信できたと思いますが、問題は、インテントに配置したデータが「別の」クラスに転送されないことです。

コードは次のとおりです: 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();


        }

     });

次に、editText を含む SQLiteExample クラスを次に示します。

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;

これが私のデータベースヘルパークラスです

 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); 

(私の以前の質問How to get data from my database and put it on a ListView that is clickable? )

更新: わかりました、私のコードは本当に面倒ですが、ずっと機能していたようです。getInfo ボタンをテストしなかったため、表示されません。お手数をおかけして申し訳ありません:pですが、ROWID(データベース)をリストビューに配置し、その値を他のクラスに配置する方法を知りたいですか?

4

1 に答える 1

0

そのはず。インテントを介して起動するクラスは、メソッドSqlLiteExampleのインテントに入れたデータを取得する必要がありますonCreate

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

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

あなたはこれをやっていますか?

于 2012-03-03T11:05:41.663 に答える