2016-12-29 8 views
0

選択したアイテムをgetView()に設定したいと考えていますが、リスト内のすべてのアイテムが選択されています。私はトーストでテストして、正しいものが表示されているので、状態が機能しています。条件は、特定の項目のDBからのエントリがtrue(選択されている)に設定されているかどうかを確認するためにチェックします。getView ListView選択したアイテムの色の変更

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    if(isItemSelected.equals("true")){ 
      listviewTitles.setBackgroundColor(0xAAAAFFFF); 
    } 
    else if (isItemSelected.equals("false")){ 
    // Default color  
    } 
} 
+1

uがlistviewTitles.setBackgroundColor(非選択色)を書きました。他の部分では –

+0

特定のセルをハイライトしたいですか?そうであれば、セルのレイアウトの背景を次のように設定することもできます:linearLayout.setBackgroundResource(COLOR.GREY); –

+0

getView()にコード全体を投稿してください---あなたがリサイクル時に選択されたアイテムが既に選択されている場合、リスト内のビューをリサイクルしているかどうか確認してください!それは何が起こっている場合! – shadygoneinsane

答えて

1

以下のような条件で背景色を更新する必要があります。

listviewTitles.setBackgroundColor(isItemSelected.equals("true") ? selectedColor : unSelectedColor); 
+0

私のために仕事をしなかった – user3175140

0

これを試して、コードでこれらの変更を加えてください。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) {  
     if (isItemSelected.equals("true")){ 
listviewTitles.setBackgroundColor(0xAAAAFFFF); 
      } 
      else if (isItemSelected.equals("false")){ 
      // Default color  
      } 
      }else{ 

     if (isItemSelected.equals("true")){ 

    listviewTitles.setBackgroundColor(0xAAAAFFFF); 
      } 
      else if (isItemSelected.equals("false")){ 
      // Default color  
      } 
     } 

私はあなたの条件のためのソリューションを持っていると信じて、この

+0

私のために仕事をしなかった – user3175140

0

はあなたのビューファイルにこのコードを貼り付けてみてください

SparseBooleanArray singleChecked; 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     if (position != singleListSelectedPosition) { 
      singleListSelectedPosition = position; 
      int totalCount = lvSingleSelect.getCount(); 
      for (int i = 0; i < totalCount; i++) { 
       if (i == position) { 
        boolean stat = singleChecked.get(position, false); 
        singleChecked.put(position, !stat); 
       } else { 
        singleChecked.put(i, true); 
       } 
      } 
      adapter.setChecked(singleChecked); 
     } 
    } 

そして、これはあなたのアダプタクラスのコードです:

public void setChecked(SparseBooleanArray ch) { 
     singleChecked = ch; 
     notifyDataSetChanged(); 
} 

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

    if (singleChecked.get(position, false)) { 
     convertView.setBackgroundColor(getResources() 
        .getColor(R.color.titlebar_background_color)); 
    } else { 
     convertView.setBackgroundColor(
        getResources().getColor(R.color.emphasis_color)); 
    } 

これに問題がある場合はいつでも教えてください。

+0

私のために仕事をしなかった – user3175140

+0

あなたはこの問題をあなたがこのコードで何を得ているのか知っていますか? – Bethan

0

私は、あなたのarraylistにデータを追加している間、ブール値のような特別な値を追加してみるべきだと思います。選択されている場合はtrue、選択されていない場合はfalseです。最初はすべてをfalseで追加します。そして、あなたは)(

int positionClicked; 

listviewTitles.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for (int i = 0; i < bean_List.size(); i++) { 
      if (i == position) { 
       positionClicked = position; 
       bean_List.get(position).setIsClicked(true); 
       notifyDataSetChanged(); 
       //Do your task here... 
      } else { 
       bean_List.get(i).setIsClicked(false); 
       notifyDataSetChanged(); 
      } 
    } 
}); 

とgetViewメソッドでlistviewTitles

をクリックして、この使用する場合: -

if (bean_List.get(position).getIsClicked() == true) { 
     listviewTitles.setBackgroundColor(0xAAAAFFFF); 
     //change color accordingly 
    } else { 
     listviewTitles.setBackgroundColor(0xAAAAFFFF); 
     //change color accordingly 
    } 
+0

私のためにDidntの仕事 – user3175140

関連する問題