2013-10-18 70 views
0

我已經使用適配器類創建了列表視圖,其中在列表項中我給出了三個按鈕。我需要更改該按鈕和其他按鈕的背景圖像。我有5個列表項目,每個項目都有自己的按鈕。當我點擊任何列表項目的按鈕時,爲什麼按鈕的背景圖像僅爲最後列表項目的按鈕更改?

現在,當我點擊第一個,第二個上升到第五列表項的按鈕,最後一個圖像的背景只改變。每個列表項的按鈕的背景圖像應該改變,如果我點擊各自的按鈕取決於邏輯。這是發生僅過去列表項..

如果有誰知道如何做到這一點,請幫助我..

代碼是:

public class MainActivity extends ListActivity { 

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

} 

private void init() { 

    LayoutInflater inflater1 = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
    View view = inflater1.inflate(R.layout.datelabel, null); 

    setListAdapter(new EventListAdapter(this));  

    ListView listView = getListView(); 
    listView.setOnItemLongClickListener(new OnItemLongClickListener() { 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 

      Toast.makeText(MainActivity.this, "tapped on position " + position, Toast.LENGTH_SHORT).show(); 

      return false; 
     } 
    }); 


} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    ((EventListAdapter)getListAdapter()).toggle(position); 

} 
private class EventListAdapter extends BaseAdapter { 
    private static final int VISIBLE = 0; 


    private static final int GONE = 8; 


    private TextView textViewForDateHeader; 
    private TextView textViewTitle; 
    private TextView textViewDialogue; 

    private TextView textViewHeader; 




    private ImageButton buttonForCheckMark; 


    private ImageButton buttonForDelete; 


    private View buttonForRemainder; 



    public EventListAdapter(Context context) 
    { 
     mContext = context; 
    } 
    public int getCount() { 
     return mTitles.length; 
    } 


    public Object getItem(int position) { 
     return position; 
    } 


    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View myView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
     myView = inflater.inflate(R.layout.activity_main, null); 


     textViewTitle = (TextView) myView.findViewById(R.id.textViewTitle); 
     textViewTitle.setText(mTitles[position]); 

     textViewDialogue = (TextView) myView.findViewById(R.id.textViewDialog); 
     textViewDialogue.setText(mDialogue[position]); 
     textViewDialogue.setVisibility(mExpanded[position] ? VISIBLE : GONE); 

     buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark); 
     buttonForCheckMark.setVisibility(mExpanded[position] ? VISIBLE : GONE); 

     buttonForDelete = (ImageButton) myView.findViewById(R.id.buttonForDelete); 
     buttonForDelete.setVisibility(mExpanded[position] ? VISIBLE : GONE); 

     buttonForRemainder = (ImageButton) myView.findViewById(R.id.buttonForRemainder); 
     buttonForRemainder.setVisibility(mExpanded[position] ? VISIBLE : GONE); 

     buttonForRemainder.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Toast.makeText(MainActivity.this, "tapped on remainder", Toast.LENGTH_SHORT).show(); 

      } 
     }); 
     buttonForCheckMark.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this, "tapped on checkMark", Toast.LENGTH_SHORT).show(); 
       buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher); 
       buttonForDelete.setBackgroundResource(R.drawable.ic_navigation_cancel); 
       buttonForCheckMark.setClickable(false); 
       buttonForDelete.setClickable(true); 
      } 
     }); 
     buttonForDelete.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this, "tapped on delete", Toast.LENGTH_SHORT).show(); 
       buttonForCheckMark.setBackgroundResource(R.drawable.ic_navigation_accept); 
       buttonForCheckMark.setClickable(true); 
       buttonForDelete.setBackgroundResource(R.drawable.ic_drawer); 
       buttonForDelete.setClickable(false); 
      } 
     }); 


     return myView; 
    } 


    public void toggle(int position) { 


     mExpanded[position] = !mExpanded[position]; 
     notifyDataSetChanged(); 


    } 

    /** 
    * Remember our context so we can use it when constructing views. 
    */ 
    private Context mContext; 


    /** 
    * Our data, part 1. 
    */ 
    //  private ImageButton[] mButtons = { 
    // 
    //    "R.drawable.remainder", 
    //    
    //  }; 
    private String[] mHeader = 
     { 
      "12 Jan, 2013", 
      "13 Feb, 2013", 
      "31 Mar, 2013",  
      "15 Aug, 2013", 
      "7 Sep, 2013" 

     }; 


    /** 
    * Our data, part 1. 
    */ 
    private String[] mTitles = 
     { 
      "Event 1", 
      "Event 2", 
      "Event 3",  
      "Event 4", 
      "Event 5" 

     }; 

    /** 
    * Our data, part 2. 
    */ 
    private String[] mDialogue = 
     { 
      "wuszuogwfuieffufuhuysugdueljwihadghgxdhgyhghsdgyigwuweyuqaGDHGYHGHGAdhgyhigxgxgeuyehu.", 
      "dgusduugyujguegytgujgdugwjhiuyg7wtqUYGYYgyijyiufufjguhgdugfhgfhgfgfhgfhghfghifgyi,dgwsdtgyfytfiuwt,", 
      "rtygygghtudggyjhgujtugdhhguyuaUUUUDJYUIDHUJHDIIDUJDHDUJHDIDIOUYhujtdugyhdgg", 
      "gjhuwjsgudggdudgjqhasdgdhgjdhushjaguhguwegagsdgygydgfgdcgycg", 
      "fhdgyhdfhfgdyhhwsddgyuduuufguugwugdfgugdgooduiuduiuduuduiuiuidudiiwdiou" 


     }; 

    /** 
    * Our data, part 3. 
    */ 
    private boolean[] mExpanded = 
     { 
      false, 
      false, 
      false, 
      false, 
      false, 
      false, 
      false, 
      false 
     }; 


} 

} 

回答

1

而是在適配器有私人的ivars的..你應該實例它們作爲finalgetView()方法內vairable。目前在監聽器裏設置了新的圖片...但是變量僅指向最後創建按鈕的地址。

更新您的代碼以喜歡這個

private class EventListAdapter extends BaseAdapter { 
    /// other things 

    //private TextView textViewForDateHeader; // remove such private vars 

    .. other methods 

    public View getView(int position, View myView, ViewGroup parent) { 
      //other code 

     final ImageButton buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark); 

      buttonForCheckMark.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher); 
       //now the buttonForCheckMark refers to the imageview your instantiated above .. not the private var 
       //other code 
      } 
     }); 

編輯

對於亮點堅持..添加適配器內部的布爾數組。

private boolean highlighted[]; 

EventListAdapter()它初始化爲mTitles現在規模過

public EventListAdapter(Context context) 
    { 
      highlighted = new boolean[mTitles.length]; 
      mContext = context; 
    } 

的onListener內..檢查高亮[clickPos] ==真..禁用它,如果它假..它設置爲真正; 你也可以使用這個值來持久getView()

+0

謝謝!!!!它正在工作......現在,當我點擊列表項時,背景圖像變化的效果消失了。如何保留這個? – user2890202

+0

檢查如果點擊你的getView又被稱爲..如果是這樣高亮值存儲在布爾VAR和getView設定高自己的立場 – Shubhank

+0

強調價值是指?請用代碼的方式建議... – user2890202

0
內突出

移動你getView方法裏面的以下聲明。

TextView textViewForDateHeader; 
TextView textViewTitle; 
TextView textViewDialogue; 
TextView textViewHeader; 
ImageButton buttonForCheckMark; 
ImageButton buttonForDelete; 
View buttonForRemainder; 
+0

謝謝!!!它現在適用於Imagebutton的最終聲明後.. – user2890202

+0

現在,當我點擊listitem,背景更改爲按鈕的效果都消失了..它應該保持不.. – user2890202

相關問題