2017-02-16 20 views
0

私はListViewをカスタムアダプタで持っています。ListViewに一致すると1つの文字列を渡して、リスト項目のテキストの色をActivityから変更します。リストビュー項目のテキストの色と画像の色を変更するには

ここに私のコード:

MyActivity:事前に

public void handleResult(String rawResult) { 
 
 
     if(Utility.isNotNull(rawResult.getText().toString())) { 
 
      for(int i=0;i<listView.getAdapter().getCount();i++){ 
 
       if(rawResult.equals(listItems.get(i).getStockItems())){
   
 
       // listView.getChildAt(i).setBackgroundColor(ContextCompat.getColor(context, R.color.hint));
  
 
        
 
        /* Here I want to change list item text color*/ 
 
        
 
        adapter.notifyDataSetChanged();
        
        
 
       } 
 
      } 
 
     } 
 
    }

ありがとう!

答えて

0

 if(listItems.get(i).getiIsSelected()==1) 
     { 
      //set red text color 
     } 
     else 
     { 
      //set black text color 
     } 
+0

のハンドラの実行を覚えてありがとうございました!!! – appu

0

UIタスクはUIスレッドでのみ実行できます。ハンドラから実行したい場合は、runOnUiThreadメソッドを定義する必要があります。これを見ては

how to use runOnUiThread

+0

を行うことがHOD私は、UIスレッド –

0

をANS取り、このコードを試してください:あなたは、あなたのアイテムを作成するとき

for(int i=0;i<listView.getChildCount();i++) { 
    // yours code 
    View item = listView.getChildAt(i); 
    item.setBackgroundResource(R.drawable.your_image); //change image 
    ((TextView)item.findViewById(R.id.text1)).setTextColor(Color.RED); //text1 is your cusotm listview item's text id 
    adapter.notifyDataSetChanged();? 
} 
0

私は、最初にあなたが彼を取得する必要があり、彼のテキストの色を変更するには、TextViewを使用すると仮定、XMLをTextView

にIDを追加

<TextView 
    android:id="@+id/myId"... 

それとも、javaの

textView.setId(R.id.myId) 

し、あなたのコード内で使用する場合:あなたは .setBackground(getResources()を使用することができ、あなたのDrawble画像付きアイテムの背景を設定したい場合は

((TextView)listView.getChildAt(i).findViewById(R.id.myId)).setTextColor(ContextCompat.getColor(context, R.color.hint)); 

をgetDrawable (R.drawable.yourDrawble));お使いのモデルクラスで

0

あなたcusomアダプタクラスチェックでIsSelectedゼロ

public void handleResult(String rawResult) {  
      if(Utility.isNotNull(rawResult.getText().toString())) { 
       for(int i=0;i<listView.getAdapter().getCount();i++){ 
        if(rawResult.equals(listItems.get(i).getStockItems())){ 
         listItems.get(i).setIsSelected(1); 
         adapter.notifyDataSetChanged();              
        } 
       } 
      } 
     } 

を初期化するこの

 public class DataHolder{ 

     private String StockItems; 
     private int isSelected; 

     public DataHolder(String StockItems, int isSelected) { 
      this.StockItems = StockItems; 
      this.isSelected = isSelected; 

     } 

     public String getStockItems() { 
      return StockItems; 
     } 

     public void setStockItems(String StockItems) { 
      this.StockItems = StockItems; 
     } 

     public int getiIsSelected() { 
      return isSelected; 
     } 

     public void setIsSelected(String isSelected) { 
      this.isSelected = isSelected; 
     } 
    } 

のような1 PARAMTERを追加するには、アダプタクラスの一つの方法を作りますテキストの色を更新し、最初にfalseのAdapterで1つのフラグを作成するには、以下を使用します。この

boolean isChangeColor = false; 
String colorCode = "#FFFFFF"; 

private void updateTextColor(boolean isChangeColor , String colorCode) { 
    this.isChangeColor=isChangeColor; 
    this.colorCode=colorCode; 
    notifyDataSetChanged();
   
} 

とgetViewメソッドで()

if(isChangeColor) { 
    textView.setTextColor(Color.parseColor(colorCode)); 
} else { 
    colorCode = "#FFFFFF"; 
    textView.setTextColor(Color.parseColor(colorCode)); 
} 
関連する問題