2012-03-18 9 views
2

私はListViewです。ユーザーがその項目の1つをクリックすると、その項目が青色になります。これを行うために、ListViewアクティビティのonCreate()メソッドでは、ユーザーのクリックに対してリスナーを設定しました。ListViewの項目の背景色を設定する際の問題

m_listFile=(ListView)findViewById(R.id.ListView01); 
     m_listFile.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
       arg0.getChildAt(arg2).setBackgroundColor(Color.BLUE); 
      } 
}); 

すべてが最初に表示されるアイテムのために正常に動作しますが、私は、リストをスクロールするとき、私はarg2値は右の項目のインデックス位置を持っている場合でも、arg0.getChildAt(arg2).setBackgroundColor(...)NullPointerExceptionをしました。私は、このアダプタを使用ListViewをロードする際

ListViewは、二行項目の構造を有する:

SimpleAdapter sa = new SimpleAdapter(
      getApplicationContext(), 
      expsList, 
      R.layout.listelement, 
      new String[] { "screen_name","text" }, 
      new int[] { R.id.Name, R.id.Value}) { 

     }; 

     m_listFile.setAdapter(sa); 

私はこの問題を解決する方法を理解していません。助けてもらえますか?

+0

は、カスタムアダプタを使用していますあなたの 'ListView'のために? – Luksprog

+0

私は最初の投稿を編集しました – Ant4res

答えて

2

言うことです

arg0.getChildAt(arg2).setBackgroundColor(Color.BLUE); 

private class MyAdapter extends SimpleAdapter { 

     public MyAdapter(Context context, List<? extends Map<String, ?>> data, 
       int resource, String[] from, int[] to) { 
      super(context, data, resource, from, to); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = super.getView(position, convertView, parent); 
      v.setBackgroundColor(Color.BLACK); //or whatever is your default color 
       //if the position exists in that list the you must set the background to BLUE 
      if(pos!=null){ 
      if (pos.contains(position)) { 
       v.setBackgroundColor(Color.BLUE); 
      } 
      } 
      return v; 
     } 

    } 
をあなたが行をクリックすると

sa = new MyAdapter(
      getApplicationContext(), 
      expsList, 
      R.layout.listelement, 
      new String[] { "screen_name","text" }, 
      new int[] { R.id.Name, R.id.Value}) { 

     }; 
m_listFile.setAdapter(sa); 

//this will hold the cliked position of the ListView 
ArrayList<Integer> pos = new ArrayList<Integer>(); 

とアダプタを設定します。

は、その後、あなたの活動にこのようにフィールドを追加

public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
        // check before we add the position to the list of clicked positions if it isn't already set 
       if (!pos.contains(position)) { 
       pos.add(position); //add the position of the clicked row 
      } 
     sa.notifyDataSetChanged(); //notify the adapter of the change  
} 
+0

すごく遅れて申し訳ありません...ありがとう、それは完璧に動作します! – Ant4res

0

は、私はあなたが

arg0.getItemAtPosition(arg2).setBackgroundColor(Color.BLUE); 

の代わりに使う必要がありますね、それは、Android開発者のリファレンスが必要ですこのようなSimpleAdapter拡張することができhere

+0

ありがとうございます。しかし、その方法でsetBackgroundColorメソッドを使用することはできません... – Ant4res

+0

アイテムをキャストできますか? – Vossi

+1

@Vossiいいえ、彼はキャストできません。 'getItemAtPosition()'はその行に関連付けられた 'data'を返します(例えば、アダプタが' Strings'を表示すると仮定した場合、このメソッドは行ビューではなく 'その行から' String'を返します)。 – Luksprog

関連する問題