2013-03-04 72 views
1

我有藥的列表,當用戶單擊該藥物之一,它會去下一個這表明藥物的細節活動..OnItemClickListener查看該列表的詳細信息

下面的代碼是與活動藥品名單..我知道我必須onclicklistener使用..但我不知道怎麼寫

public class MedicineView extends Activity 
{ 
private SQLiteDatabase database; 
private ArrayList<String> result = new ArrayList<String>(); 
private ArrayList<Long> idList = new ArrayList<Long>(); 
private ArrayAdapter<String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.medicine_view); 
    ListView listContent = (ListView)findViewById(R.id.listView1); 

    DBHelper helper = new DBHelper(this); 
    database = helper.getWritableDatabase(); 

    // view data 

    try{ 
     String query = "select * from " + DBHelper.TABLE_MEDICINE; 

     Cursor c = database.rawQuery(query, null); 

     if(c!=null) 
     { 
      c.moveToFirst(); 
      int getIndex = c.getColumnIndex(DBHelper.MED_ID); 
      int getNameIndex = c.getColumnIndex(DBHelper.MED_NAME); 
      int getDosageIndex = c.getColumnIndex(DBHelper.DOSAGE); 
      int getFrequencyIndex = c.getColumnIndex(DBHelper.FREQUENCY); 

      if(c.isFirst()) 
      { 
       do{ 
        idList.add(c.getLong(getIndex)); 
        result.add(c.getString(getNameIndex)+" | " + c.getString(getDosageIndex) 
          +" | " + c.getString(getFrequencyIndex)+"\n"); 
       }while(c.moveToNext()); 
      } 
     } 

     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,result); 
     listContent.setAdapter(adapter); 
     listContent.setTextFilterEnabled(true); 

     c.close(); 

    } 
    catch(SQLException e){ 
    } 

listContent.setOnItemClickListener(this); 
} 

public void onClickHome(View v){ 
    startActivity (new Intent(getApplicationContext(), MenuUtama.class)); 
} 

public void onClickAdd(View v){ 
    startActivity (new Intent(getApplicationContext(), MedicineAdd.class)); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
    Intent myIntent = new Intent(v.getContext(), MedicineDetail.class); 
    myIntent.putExtra("position", position); 
    startActivityForResult(myIntent, 0); 
} 
} // end class 

那麼這裏就是我的detailsactivity

public class MedicineDetail extends Activity 
{ 
private SQLiteDatabase database; 
private ArrayList<Long> idList = new ArrayList<Long>(); 
ArrayList<String> list = new ArrayList<String>(); 

Button btnEdit; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.medicine_detail); 

    int id = getIntent().getIntExtra("id", 2); //i don't know how to do here 

    btnEdit = (Button)findViewById(R.id.imageButton2); 

    DBHelper helper = new DBHelper(this); 
    database = helper.getWritableDatabase(); 

    String sql = "select * from " + DBHelper.TABLE_MEDICINE + " WHERE id = " + id; 
    Cursor cursor = database.rawQuery(sql, null); 

    try{ 
     if(cursor != null){ 
      if (cursor.moveToFirst()) { 
       do { 
        idList.add(cursor.getLong(0)); 
        list.add(cursor.getString(1)); 
        list.add(cursor.getString(2)); 
        list.add(cursor.getString(3)); 
        list.add(cursor.getString(4)); 
        list.add(cursor.getString(5)); 
        list.add(cursor.getString(6)); 
       } while (cursor.moveToNext()); 
      } 
     } 
    } 
    catch(SQLException e){ 
    } 

    cursor.moveToFirst(); 

    TextView StartDate = (TextView) findViewById(R.id.textView8); 
    TextView EndDate = (TextView)findViewById(R.id.textView9); 
    TextView MedName = (TextView)findViewById(R.id.textView10); 
    TextView Dosage = (TextView)findViewById(R.id.textView11); 
    TextView Frequency = (TextView)findViewById(R.id.textView12); 
    TextView Instruction = (TextView)findViewById(R.id.textView13); 

    StartDate.setText(cursor.getString(1)); 
    EndDate.setText(cursor.getString(2)); 
    MedName.setText(cursor.getString(3)); 
    Dosage.setText(cursor.getString(4)); 
    Frequency.setText(cursor.getString(5)); 
    Instruction.setText(cursor.getString(6)); 
    cursor.close(); 

} 

public void onClickHome(View v){ 
    startActivity (new Intent(getApplicationContext(), MedicineView.class)); 
} 

} // end class 

我不知道該怎麼做,任何人PLZ幫助

回答

1

你應該讓你的類實現onItemClickListener

implements AdapterView.OnItemClickListener

onCreate添加以下行:

listContent.setOnItemClickListener(this); 

然後覆蓋:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
{ 
    Intent intent = createIntentwithExtra(v, position); 
    startActivity(intent); 
} 

UPDATE:

public Intent createIntentwithExtra(View v, int position) 
{ 
    Intent intent = new Intent(getApplicationContext(), TaskDetailsActivity.class); 
    intent.putExtra(KEY_USERNAME, username); 
    intent.putExtra(KEY_ID, tasksRepository.get(position).getId()); 
    return intent; 
} 
+0

我可以去其他活動,但我要如何通過ID太@覆蓋 \t公共無效onItemClick(適配器視圖父母, View v,int position,long id){ \t \t // TODO自動生成方法存根 \t \t意圖i =新意圖(v.getContext(),Medic ineDetail.class); startActivity(i); \t \t \t} – new 2013-03-04 17:14:35

+0

我已經檢查,它傳遞的位置,但我多麼想找回在另一個活動位置 – new 2013-03-04 17:28:30

+0

你可以傳遞的意圖的位置。正如你所看到的,我在這種情況下使用函數createIntentWithExtra創建了一個intent並傳遞了位置和視圖,我添加了這個函數以便你可以使用它,查看更新後的答案。 – 2013-03-04 18:40:37

1

使用onItemSelectedListener:arg0都將是ListView和ARG2將在適配器中的項目位置

@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){ 
}