2012-03-19 53 views
0

在下面的代碼中,我想在我的行的某些「元素」上設置Onclick監聽器。我應該在哪裏放置setOnclick監聽器代碼?在活動代碼或適配器中?我想第一個,但在那種情況下,我將如何確定哪一行已被選中 (每行應該讓我看到不同的意見)?從列表視圖行進入另一個屏幕

Java代碼:

List<String[]> info=new ArrayList<String[]>(); 
     String[] names_info=new String[5]; 
     String[] phones_info=new String[5]; 
     String[] map_info=new String[5]; 
     String[] web_info=new String[5]; 
     String[] photos_info=new String[5]; 
for(int i=0;i<names_info.length;i++) 
    { 
     info.add(new String[]{names_info[i],phones_info[i],map_info[i],web_info[i],photos_info[i]}); 
    } 
    for (int i = 0; i < info.size(); i++) { 
     Log.d("TAG", "item " + i + 
       " name:" + info.get(i)[0] + 
       " info:" + info.get(i)[1]); 
    } 
    FacilitiesAdapter adapter = new FacilitiesAdapter(this,info,want_info_display,want_phone_display,want_photos_display); 
    //System.out.println(info[0]); 
    setListAdapter(adapter); 

EDIT 

ListView lv = getListView(); 



lv.setTextFilterEnabled(true); 

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // When clicked, show a toast with the TextView text 
      //Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), 
      //Toast.LENGTH_SHORT).show(); 
Intent i33 = new Intent(HotelsScreen.this,AbousUsScreen.class); 
      i33.putExtra("position", arg2); 
      System.out.println(arg2); 
      System.out.println(info.get(arg2)[0]); 

     startActivity(i33);    
     } 
    }); 

我看到日誌的結果,這樣就意味着我的信息通常是獲取其結果。

這是我的適配器。

public class FacilitiesAdapter extends ArrayAdapter<String[]> { 
    private final Context context; 
    List <String[]> dataList; 

    public FacilitiesAdapter(Context context, List<String[]> dataList, int need_phone, int need_info, int need_photos) { 
     super(context, R.layout.expand_row); 
     this.context = context; 
     this.dataList = dataList; 
     //this.tracks_condition=tracks_condition; 
     //this.count=count; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View rowView = inflater.inflate(R.layout.expand_row, parent, false); 
      System.out.println("I am here"); 
      String[] data = dataList.get(position); 
      String name=data[0]; 
      TextView textView = (TextView) rowView.findViewById(R.id.name); 
      System.out.println("I am in the adapter "+name); 
      textView.setText(name); 
      return rowView; 
    } 
} 

編輯1: 我我現在應該在每一個項目,看我點擊它的位置,它的名字?因爲我什麼都看不到?

回答

1

可這些線這麼簡單的東西獨自:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
     long arg3) 
    { 
    Intent i33 = new Intent(MyScreen.this,OtherScreen.class); 
    i33.putExtra("position", position); 
    startActivity(i33); 
    } 
}); 
+0

這可能會去你的onCreate – dldnh 2012-03-20 13:43:49

+0

好的這一點。我怎麼能作爲參數傳遞文本的ID(名稱和信息,我命令使用它們,以便知道如果'onItemClick'訪問與適配器具有相同的'dataList',可以打開 – 2012-03-20 13:46:47

+1

的Whick屏幕,它可以獲取它需要的數據,然後使用'i33.putExtra(...)'類似於上面顯示的 – dldnh 2012-03-20 13:55:47

0

我覺得你最大的問題將被找出如何一行映射到相應的屏幕。如果你可以做到這一點,你可以設置一個包含所有映射的Map對象。

在您的適配器中,您可以在視圖上使用setTag(),並將它從Map返回的內容傳遞給它,然後從中創建一個新的Intent。

//In your adapter's getView() method... 
Class activityClass = rowToActivityMap.get(someRowAttribute); 
view.setTag(activityClass); 

//In your click listener... 
Class activityClass = (Class)view.getTag(); 
Intent intent = new Intent(this, activityClass); 
startActivity(intent); 
相關問題