2011-11-24 21 views
1

データベースからデータを取り込んでいるアプリケーションでリストビューを使用しています。私がしたいことは、たとえば、単一のリストビュー項目のテキストの色を赤に設定するための計算に依存します。それを行う方法はありますか?ここでAndroidがリストビューの項目に色を設定しました

は私がリストビューを移入しています方法です:

Cursor cursor = dbHelper.executeSQLQuery(sql); 
    if(cursor.getCount()==0){ 
     Log.i("Cursor Null","CURSOR IS NULL"); 
     nocards.setVisibility(View.VISIBLE); 
    } else if(cursor.getCount()>0){ 
     nocards.setVisibility(View.GONE); 

     for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) { 
       text = cursor.getString(cursor.getColumnIndex("Title")); 
       Log.i("Show Title","Show Title : "+text); 
       collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("CollID"))); 
       Log.i("CollId","Collection ID : "+collId); 

       if (isLoggedIn) { 
        for(int k=0; k<ObjectID.size(); k++){ 
         if (ObjectID.get(k) == collId){ 
          cardsCount = OwnedCardsCount.get(k); 
         } 
        } 
       } else { 
         cardsCount = cursor.getString(cursor.getColumnIndex("TotalCards")); 
       } 

       String sqlite2 = "SELECT media_id FROM collection_media WHERE collection_id="+collId+" AND media_type_id="+fronImage; 
       Cursor bg = dbHelper.executeSQLQuery(sqlite2); 
       if (bg.getCount() == 0) { 
        bg.close(); 
       } else if (bg.getCount() > 0) { 
        for (bg.move(0); bg.moveToNext(); bg.isAfterLast()) { 

         int mediId = Integer.parseInt(bg.getString(bg.getColumnIndex("media_id"))); 
         Log.i("","mediId : " + mediId); 
         //if(storageID==1){ 
          path = rpc.getPublicPathsInternal(servername, fronImage, mediId, Recommended.this); 
         /*} else if(storageID==2){ 
          path = rpc.getPublicPathsExternal(servername, fronImage, mediId); 
         }*/ 
         Log.v("","path: "+path); 
        } 
       } 


       array.add(collId); 
       hm = new HashMap<String, Object>(); 
       hm.put(IMAGE, path); 
       hm.put(TITLE, text); 
       hm.put(CARDS_COUNT, cardsCount +" Stampii"); 
       items.add(hm); 
     } 
    } 

     final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview, 
       new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img}); 
     lv1.setAdapter(adapter); 
     lv1.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> a, View v, int position, long id) 
      { 
       Intent previewMessage = new Intent(Recommended.this, MyCollectionId.class); 
       TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
       previewMessage.putExtra("collection_id", array.get(position)); 
       previewMessage.putExtra("opentype", 1); 
       parentActivity.startChildActivity("MyCollectionId", previewMessage); 
      } 
     }); 

と私のmain_listview.xmlのコード:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 

     <ImageView   
      android:id="@+id/main_img" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_margin="4dp"/> 


     <TextView 
      android:id="@+id/main_name" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="14dp" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:layout_toRightOf="@+id/main_img" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp"/> 

     <TextView 
      android:id="@+id/main_info" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/main_name" 
      android:layout_toRightOf="@+id/main_img" 
      android:textSize="12dp" 
      android:textColor="#666666" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp"/> 

     <ImageView 
      android:id="@+id/arrow" 
      android:src="@drawable/ic_arrow_right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="10dp" 
      android:layout_centerVertical="true" /> 


    </RelativeLayout> 

</LinearLayout> 

だから私は赤にmain_nameのの、textColorを設定します。助言がありますか?

+1

1 snap = 1000 words。 –

+0

カスタムアダプターを使用する..カーソルアダプターを拡張する –

答えて

2

を楽しむことができます。このようなクラスが必要です。

public class StillSimpleAdapter extends SimpleAdapter { 

    public StillSimpleAdapter(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 view = super.getView(position, convertView, parent); 
    if (timeForRed(position)) ((TextView) view.findViewById(R.id.main_name)).setTextColor(0xffff0000); 
    return view; 
    } 

} 

コード内でSimpleAdapterの代わりに使用し、どこかでtimeForRed()メソッドを実装すれば満足できます。

1

単一のリスト項目のXMLを作成します。その後、getView()機能を無効にする独自のアダプタを作成します。この関数では、リストアイテムのレイアウトを拡張し、データからコントロールを設定し、必要に応じてテキストビューの色を設定します。

0

BaseAdapterクラスを拡張するクラスを作成できます。 BaseAdapterクラスはいくつかのオーバーライドメソッドを提供します。 getView()がその1つです。この方法では、必要に応じてビューをカスタマイズすることができます。

以下は、このサンプルプロジェクトです。あなたは考えを得ることができます:

https://ujjwal.opendrive.com/files?51477570_t4x6y

関連する問題