2015-05-11 54 views
0

我有一個使用CustomAdapter的listView,listView Item包含一個按鈕,我在CustomAdapter中創建了一個OnClickListener來處理這個按鈕,這取決於它的位置,使用switch(position)購買並且所有的案例都工作正常除了最後一個案例4,當我點擊它OnClickListener調用案例0.我認爲案件4的代碼中有東西,使應用程序做的情況下,0的指示,所以我在開始時添加了一個日誌的情況4,但我發現它被調用,情況0被調用。我也試着刪除內側箱4的所有代碼和它的工作的點擊,如果我在電話的情況下再次單擊0OnClickListener調用錯誤的案例

請如果有誰知道如何解決這個問題,請大家幫忙,謝謝

這裏我CustomAdapter:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View row = convertView; 
    MyViewHolder holder = null; 
    if(row==null) 
    { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.listview_item, parent, false); 
     holder = new MyViewHolder(row); 
     row.setTag(holder); 
     final SharedPreferences shared = context.getSharedPreferences("PrefName", Context.MODE_PRIVATE); 
     final SharedPreferences.Editor editor = shared.edit(); 
     ........ 

     Button changeSet = (Button) row.findViewById(R.id.setBtn); 
     changeSet.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       switch (position){ 
        case 0: 
         Log.d("NOTHING", "Case 1 called"); 
         editor.putBoolean("Clicked", true); 
         editor.putString("TheNumber", "1"); 
         editor.apply(); 
         Toast.makeText(context, "Set 1 selected please press the save button", Toast.LENGTH_SHORT).show(); 
         break; 

        ........... 

        case 4: 
         Log.d("NOTHING", "Case 5 called"); 
          Toast.makeText(context, "Set is Locked", Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }); 
    } 
    else 
    { 
     holder = (MyViewHolder) row.getTag(); 
    } 

    holder.myImage.setImageResource(images[position]); 
    holder.myText.setText(titleArray[position]); 
    holder.myText2.setText(titleArray2[position]); 


    return row; 
} 
+1

它很好,如果你張貼按鈕的點擊與開關塊碼。 – user370305

+0

@ user370305我添加了代碼 –

+0

移動onClickListener。將onClickListener代碼放在「holder.myImage.setImageResource(images [position]);」之前。 – Carnal

回答

0

您應該設置OnClickListener everytime.Like這樣的:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
View row = convertView; 
ViewHolder holder = null; 
if(row==null){ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    row = inflater.inflate(R.layout.listview_item, parent, false); 
    holder = new ViewHolder(); 
    holder.changeSetButton = (Button) row.findViewById(R.id.setBtn); 
    holder.myImage =...; 
    holder.myText2 = ...; 
    row.setTag(holder); 
} else { 
    holder = (ViewHolder)row.getTag(); 
} 

final SharedPreferences shared = context.getSharedPreferences("PrefName", Context.MODE_PRIVATE); 
final SharedPreferences.Editor editor = shared.edit(); 
........ 

holder.changeSetButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     switch (position){ 
      case 0: 
       Log.d("NOTHING", "Case 1 called"); 
       editor.putBoolean("Clicked", true); 
       editor.putString("TheNumber", "1"); 
       editor.apply(); 
       Toast.makeText(context, "Set 1 selected please press the save button", Toast.LENGTH_SHORT).show(); 
       break; 

      ........... 

      case 4: 
      Log.d("NOTHING", "Case 5 called"); 
      Toast.makeText(context, "Set is Locked", Toast.LENGTH_SHORT).show(); 
      break; 
     } 
    } 
}); 
// 
holder.myImage.setImageResource(images[position]); 
holder.myText.setText(titleArray[position]); 
holder.myText2.setText(titleArray2[position]); 

return row; 
} 

static class ViewHolder{ 
    Button changeSetButton; 
    ImageView myImage; 
    TextView myText2; 
}