2014-10-16 86 views
-2

我有一個問題,做這個android項目。你能幫我解決這個問題,我想改變onItemClick上的按鈕文字。Android的列表視圖itemclick更改文本按鈕

這是我CustomerAdapter:

@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 
     holder = new ViewHolder(); 

     holder.member_name = (TextView) convertView 
       .findViewById(R.id.member_name); 
     holder.product_icons = (ImageView) convertView 
       .findViewById(R.id.product_icons); 
     holder.status = (TextView) convertView.findViewById(R.id.status); 
     holder.addtocart = (Button) convertView 
       .findViewById(R.id.btnaddtocart); 

     convertView.setTag(holder); 

     RowItem row_pos = rowItems.get(position); 

     holder.product_icons.setImageResource(row_pos.getProfile_pic_id()); 
     holder.member_name.setText(row_pos.getMember_name()); 
     holder.status.setText("P" + row_pos.getStatus()); 
     holder.addtocart.setText(" add to cart"); 


     } else { 

      holder = (ViewHolder) convertView.getTag(); 

     } 

     return convertView; 

這裏是我的MainActivity.java代碼包含onitemclick listerner

 public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 

    RowItem item = rowItems.get(position); 
    String member_name = item.getMember_name(); 
     int product_icons = item.getProfile_pic_id(); 
     String status = item.getStatus(); 

    Button cart= (Button) view.findViewById(R.id.btnaddtocart); 
    cart.setText("Added to cart"); 


    } 
+0

與你的這段代碼會發生什麼最初兄弟?如果您想更改文字,請在初始化後直接設置文字以及爲什麼切換? – Elltz 2014-10-16 12:37:18

回答

0

讓我殺了你的問題..

1:獲取按鈕,當您單擊列表視圖項

ANS變化:按鍵響應單擊事件..所以,如果你把它放在列表視圖需要單擊事件從列表視圖..所以克服它 使

ListView focusable android:focusable="true" 
Button not focusable android:focusable="false" 

,並添加此

android:descendantFocusability="beforeDescendants" 
在列表視圖

..它使ListView控件及其子之前接收焦點...你現在移動到點擊的事件,這是你的代碼的編輯版本

public void onItemClick(AdapterView<?> parent, View view, int position, 
    long id) { 

    RowItem item = rowItems.get(position); 
    String member_name = item.getMember_name(); 
    int product_icons = item.getProfile_pic_id(); 
    String status = item.getStatus(); 

    Button cart= (Button) view.findViewById(R.id.btnaddtocart); 
    cart.setText("Added to cart"); 
    Toast.makeText(this.getApplicationContext(),"Added to Cart: " + member_name, Toast.LENGTH_SHORT).show(); 

} 

我刪除了一些代碼清潔和也我刪除此代碼

newadapter = new CustomAdapter(this, rowItems); 
mylistview.setAdapter(newadapter); 
newadapter.notifyDataSetChanged(); 

,因爲它設置適配器一遍,你不要看到按鈕被設置 並刷新它also..lol ..所以這是我的球場,試試吧並讓我知道如果它的作品 也如果你添加項目到你的列表查看或刪除項目,你想展示給你不需要用戶 重新設置適配器只需要調用notifyDataSetChanged()方法..

EIDT 2:有關禁用項目

listview.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
         // TODO Auto-generated method stub 
         if(cart.getText().equals("Added to cart")){ //checking the text of button 
          arg1.setFocusable(false); 
          arg1.setFocusableInTouchMode(false); 
          arg1.setAlpha((float)0.2);// i dont trust this line-(it works) but i want to give you the idea, there are soo many ways of making a view transparent. 
         } 

        } 

這使得點擊事件不會響應焦點,並將其設置爲透明..但如果您只想按鈕不響應點擊事件,setEnabled爲false,並且還將焦點設置爲false ..在按鈕「cart」上。我用記事本寫了這樣檢查的代碼的正確性或任何作弄愚蠢

幸福代碼

+0

非常感謝! – 2014-10-17 12:22:19

+0

我想添加其他問題。我想在改變我的按鈕和禁用項目點擊位置的文本後實現。非常感謝你。 – 2014-10-17 12:40:01

+0

禁用項目點擊爲listview或buttom ?? @ paulbuscano或禁用響應點擊事件的特定項目? – Elltz 2014-10-17 13:27:25

0

你的switch語句設置文本只有被點擊項目的索引是0(督察列表/網格視圖中的第一項)。點擊任何其他項目都不會對你的textview做任何事情。如果您只是想在點擊某些內容時設置文本,請使用cart.setText(「Hello」)替換switch語句。

+0

我會做什麼?我想實現的是當我點擊一個列表視圖上的特定項目時,我想單擊它後更改我的按鈕的文本。 – 2014-10-17 00:14:57