2012-06-21 62 views
1

列表視圖onitemclicklistener我有一個listview自定義佈局

<ListView 
     android:clickable="true" 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

那裏面去的元素是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 




    <TextView 
     android:id="@+id/textview_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.76" 
     android:text="Choose" 
     android:clickable="true" /> 


    <EditText 
     android:id="@+id/edittext_share" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.12" 
     android:ems="10" 
     android:hint="share" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 


    <EditText 
     android:id="@+id/edittext_spent" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.11" 
     android:ems="10" 
     android:hint="spent" 
     android:inputType="number" /> 

</LinearLayout> 

我想,當我點擊TextView,事情應該發生。我通過getListView()設置了setOnItemClickListener,但它不起作用。

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this,"clicked id="+arg2, Toast.LENGTH_SHORT).show(); 
} 

 ListView lv = getListView(); 
    lv.setOnItemClickListener(this); 

我該怎麼做才能讓TextView的clicable?

編輯:我的適配器代碼:

private class PeopleListAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 
    public Cursor cursor; 
    public ArrayList<PersonInfo> peopleList; 
    public ArrayList<PersonInfo> unsavedPeopleList; 

    public PeopleListAdapter(Context context, Cursor cursor) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
     this.cursor = cursor; 
     peopleList = new ArrayList<PersonInfo>(cursor.getCount()); 
    } 

    public PeopleListAdapter(Context context, Cursor cursor, 
      ArrayList<PersonInfo> unsavedList) { 
     this(context, cursor); 
     this.unsavedPeopleList = unsavedList; 
    } 

    /** 
    * The number of items in the list is determined by the number of 
    * speeches in our array. 
    * 
    * @see android.widget.ListAdapter#getCount() 
    */ 
    public int getCount() { 
     return peopleList.size() + unsavedPeopleList.size(); 
    } 

    /** 
    * Since the data comes from an array, just returning the index is 
    * sufficent to get at the data. If we were using a more complex data 
    * structure, we would return whatever object represents one row in the 
    * list. 
    * 
    * @see android.widget.ListAdapter#getItem(int) 
    */ 
    public Object getItem(int position) { 

     return position; 
    } 


    /** 
    * Use the array index as a unique id. 
    * 
    * @see android.widget.ListAdapter#getItemId(int) 
    */ 
    public long getItemId(int position) { 
     return position; 
    } 

    /** 
    * Make a view to hold each row. 
    * 
    * @see android.widget.ListAdapter#getView(int, android.view.View, 
    *  android.view.ViewGroup) 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid 
     // unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is 
     // no need 
     // to reinflate it. We only inflate a new View when the convertView 
     // supplied 
     // by ListView is null. 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list_item_person_info, 
        null); 

      // Creates a ViewHolder and store references to the two children 
      // views 
      // we want to bind data to. 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView 
        .findViewById(R.id.textview_name); 
      holder.share = (EditText) convertView 
        .findViewById(R.id.edittext_share); 
      holder.spent = (EditText) convertView 
        .findViewById(R.id.edittext_spent); 
      convertView.setTag(holder); 

     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     PersonInfo p = getPersonInfo(position); 
     holder.name.setText(p.name); 
     holder.name.setTag(p.lookupUri); 
     holder.share.setText(p.share + ""); 
     holder.spent.setText(p.spent + ""); 

     return convertView; 
    } 

    PersonInfo getPersonInfo(int position) { 
     if (position < peopleList.size()) { 
      PersonInfo pInfo = peopleList.get(position); 
      if (pInfo == null) { 
       pInfo = new PersonInfo(); 
       cursor.moveToPosition(position); 
       pInfo.lookupUri = Uri.parse(cursor.getString(0)); 
       Cursor c = NewEventActivity.this.getContentResolver() 
         .query(pInfo.lookupUri, 
           new String[] { Contacts._ID, 
             Contacts.DISPLAY_NAME }, null, 
           null, null); 
       pInfo.share = cursor.getInt(1); 
       pInfo.spent = cursor.getInt(2); 
       try { 
        if (c.moveToFirst()) { 
         pInfo.name = c.getString(1); 
        } 
       } finally { 
        cursor.close(); 
       } 
       peopleList.set(position, pInfo); 

      } 

      return pInfo; 
     } else { 
      return unsavedPeopleList.get(position - peopleList.size()); 
     } 
    } 
+0

請爲您提供適配器代碼....... –

+0

你試過'onListItemClick' – Praveenkumar

+0

仍然你的問題沒有解決了嗎? – Praveenkumar

回答

2

您需要的點擊監聽器監聽器設置爲TextView的適配器在getView()函數......

看到這link

 private class PeopleListAdapter extends BaseAdapter implements 
OnClickListener { 

    .............................. 
     ............................ 


public View getView(int position, View convertView, ViewGroup parent) { 
        
      ...................... 
      ..................... 
       holder.name.setText(p.name); 
        //holder.name.setTag(p.lookupUri); 
        holder.share.setText(p.share + ""); 
        holder.spent.setText(p.spent + ""); 

     holder.name.setOnClickListener(this); 
     holder.name.setTag(position); 



        return convertView; 
    } 

    @Override 
    public void onClick(View v) { 
     Log.d("Sample", "Clicked on tag: " + v.getTag()); 
     ////get PersonInfo using getPersonInfo(position) 
    } 
+0

一些代碼會很棒.. :) – prongs

+0

他在使用'listactivity',那麼'getView()'函數在那裏呢? – Praveenkumar

+0

確定....... 2分鐘請寫...... –

0

Set on ClickListener在你的getListView()方法d TextView。

謝謝。

2

如果您使用的是ListActicvity,那麼有一個可用的默認方法,稱爲onListItemClick()。你必須重寫此方法在您的ListActivity,

protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) { 

     //find which item is clicked using the view object 
    }; 
+0

'\t @覆蓋 \t保護無效onListItemClick(ListView的升,視圖V,INT位置,長ID){ \t \t // TODO自動生成方法存根 \t \t Toast.makeText(這一點, 「喜」,麪包。 LENGTH_LONG).show(); \t \t super.onListItemClick(l,v,position,id); \t} ' – prongs

+0

對不起。 cudn't得到它 –

+0

我的意思是,它不工作。 – prongs

0

據我瞭解,這是你想要什麼:你想ListView控件內TextView的是點擊執行基於某些操作,和你有使用ListView

所以裏面的自定義佈局項目:因爲它增加了一個監聽器爲ListView內,而不是裏面的元素的整個項目setOnItemClickListener 將無法​​正常工作

爲了達到這個目的,你可以這樣做: 爲你的ListView中的元素添加一個標籤,並在getView方法中爲你提供默認位置,使用getTag()方法使用標籤可以區分什麼被點擊,並在XML中,你可以簡單地聲明onClick(),並提供其定義