2013-10-18 1 views
0

나는 목록 항목에서 3 개의 단추가있는 어댑터 클래스로 목록보기를 만들었습니다. 그 버튼의 배경 이미지와 다른 버튼도 바꿔야합니다. 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

을 대신 어댑터에 개인 인스턴스 변수를 필요없이 .. 당신은 getView() 메소드 내에서 final으로 변환 할 수 있어야합니다. 현재 청취자 내부에서 새로운 이미지를 설정합니다 ... 그러나 변수는 마지막으로 생성 된 버튼의 주소만을 가리 킵니다.

업데이트 코드는 .. 계속 하이라이트은이

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 
      } 
     }); 

편집

을 좋아하는 어댑터 내부 부울 배열을 추가 할 수 있습니다. EventListAdapter()가 mTitles로 초기화 내부

private boolean highlighted[]; 

가 있습니다 .. onListener 내부 지금도

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

크기를 확인 강조가 거짓 ..로 설정하면 [clickPos] == 사실 .. 비활성화 및 참된; 이 값을 사용하여 내부의 강조 표시를 유지할 수도 있습니다. getView()

+0

감사합니다 !!!! 그것은 작동 중입니다 ... 지금, 목록 항목을 탭하면 배경 이미지 변경의 효과가 사라졌습니다. – user2890202

+0

도청되었을 때 getView가 다시 호출되는지 확인하십시오. bool var에 강조 값을 저장하고 getView에서 강조 표시 위치를 직접 지정하십시오. – Shubhank

+0

강조 표시된 값은 ?? 코드 방법으로 제안 해주세요 ... – user2890202

0

getView 메서드 내에서 다음 선언을 이동하십시오.

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

감사합니다 !!! 이제 Imagebutton에 대한 final을 선언 한 후 작동합니다. – user2890202

+0

이제 listitem을 클릭하면 버튼에 대한 배경 변경 효과가 사라졌습니다. – user2890202

관련 문제