2016-05-11 1 views
2

私は、カスタムアダプタとAndroidの中で実験しました:私のMainActivityは1 TextViewと行あたりImageViewListViewが含まれています。 ImageView色が表示されます。私のモデルはで、名前はString、鍵はintです。 私のAdapterは、数字(キー)ごとに異なる背景色をImageViewに設定する必要があります。キーが4の場合、ImageViewが点滅します。 私のコードを実行すると、Listの最初の要素も点滅しますが、キーは4ではありません。 私が間違っていることを誰かが私に説明してくれることを願っています。Androidのカスタム・アダプタは - 私はこの問題の上にstumbeldたときに、間違った項目のアニメーション

public class myAdapter extends ArrayAdapter<Test>{ 

    private ArrayList<Test> liste; 

    public myAdapter(Context context, int resource, ArrayList<Test> items) { 
     super(context, resource, items); 
     liste = items; 
    } 

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

     if(convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      convertView = inflater.inflate(R.layout.list_view_item_main, parent, false); 
     } 

     Test t = getItem(position); 
     TextView tv = (TextView) convertView.findViewById(R.id.lvName); 
     ImageView iv = (ImageView) convertView.findViewById(R.id.lvStatus); 

     int k = t.getKey(); 
     tv.setText(t.getName()); 

     if (k == 1) { 
      iv.setBackgroundColor(Color.rgb(102, 255, 51)); 
     } 

     if (k == 2) { 
      iv.setBackgroundColor(Color.rgb(255, 204, 51)); 
     } 

     if (k == 3) { 
      iv.setBackgroundColor(Color.rgb(245, 61, 0)); 
     } 

     if (k == 4) { 
      iv.setBackgroundColor(Color.rgb(245, 61, 0)); 
      final Animation animation = new AlphaAnimation(1, 0); 
      animation.setDuration(1000); 
      animation.setInterpolator(new LinearInterpolator()); 
      animation.setRepeatCount(Animation.INFINITE); 
      animation.setRepeatMode(Animation.REVERSE); 
      iv.startAnimation(animation); 
     } 


     return convertView; 


    } 

} 

public class Test { 

    private String name; 
    private int key; 


    public Test(String name, int key) { 
     this.name = name; 
     this.key = key; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getKey() { 
     return key; 
    } 

    public void setKey(int key) { 
     this.key = key; 
    } 
} 


public class MainActivity extends AppCompatActivity { 

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

     ArrayList<Test> liste = new ArrayList<>(); 
     liste.add(new Test("Milch", 1)); 
     liste.add(new Test("Käse", 2)); 
     liste.add(new Test("Schokolade", 3)); 
     liste.add(new Test("Capri Sonne", 4)); 

     ListView lv = (ListView) findViewById(R.id.lvShowFridge); 
     myAdapter mya = new myAdapter(this, R.layout.list_view_item_main, liste); 
     lv.setAdapter(mya); 
    } 

} 
+0

背景色は期待通りに変わりますか? – bwegs

+0

はい、背景色が変化しています。しかし、色を取り除くと(k == 1)色が設定されていないと思います。私の場合は、(k == 4)の色を設定しています。 – Joey

答えて

2

getView(...)に渡されたときconvertViewがnullでない場合)の項目のビューが再使用されていることに注意してください。一度item1を表示するために使用された特定のビューは、に渡され、item4と表示されます。つまり、item1の呼び出しで設定したプロパティはすべて、背景とアニメーションを含めて有効です。

どのような影響がありますか?まあ...:

1 - あなたは、具体的な背景を設定していない場合、あなたが言ったように、それはあなたがたときのk == 1背景を設定取るならば、それは意志、あなたのケースで(前のものを維持しますk == 4で呼び出されたときにアニメーションを開始していますが、ビューが存在する場合は停止していません。他の色/インデックスのために再利用されます。 iv.clearAnimation()へのコールを適切に追加します(アニメーションが不要な場合)。

+0

それだけです。どうもありがとうございました :) – Joey

関連する問題