2013-02-16 98 views
0

我使用簡單的遊標適配器從列表視圖中使用自定義佈局獲取所有記錄。我想顯示優先級文本視圖來改變顏色根據文字,如紅色,如果優先級高,綠色如果中等和黃色,如果low.is有一些辦法做到這一點。我很新的android動態更改listview項目中的textview顏色

我的代碼爲listview

myList=(ListView) findViewById(R.id.listView1); 
    myAdapter.open(); 

    Cursor cursor = myAdapter.fetchAllView(); 

    startManagingCursor(cursor); 
String []from=new String[]{ DbAdapter.KEY_DESCRIPTION,DbAdapter.KEY_TITLE,DbAdapter.KEY_TASK_PRIORITY,DbAdapter.KEY_CATEGORY_NAME,DbAdapter.KEY_TIME,DbAdapter.KEY_DATE }; 
int[] to=new int[] { R.id.txtdescription,R.id.txtitem,R.id.txtPriority,R.id.txtcategory,R.id.txttimeOne,R.id.txtDateOne}; 
myCursorAdapter = new SimpleCursorAdapter(ListItems.this, R.layout.items, cursor,from, to); 
myList.setAdapter(myCursorAdapter); 

列表視圖中工作正常,但如何編寫代碼來改變TextView的顏色列表視圖項

在此先感謝

回答

0

您需要使用使用自定義光標適配器,因爲你可以改變的事情dyanamically

這裏我給一個例子,在bindView方法改變文本的顏色

public class MessageAdapter extends CursorAdapter { 
private Cursor mCursor; 
private Context mContext; 
private final LayoutInflater mInflater; 


public MessageAdapter(Context context, Cursor c) { 
    super(context, c); 
    mInflater=LayoutInflater.from(context); 
mContext=context; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    TextView mobileNo=(TextView)view.findViewById(R.id.mobileNolistitem); 
    mobileNo.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_MOBILENO))); 

    TextView frequency=(TextView)view.findViewById(R.id.frequencylistitem); 
    frequency.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_FREQUENCY))); 

    TextView rowid=(TextView)view.findViewById(R.id.rowidlistitem); 
    rowid.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_ID))); 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view=mInflater.inflate(R.layout.message_list_item,parent,false); 
    return view; 
} 

}

+0

先生即時從SQlite數據庫視圖獲取數據到列表視圖,我想通過代碼 – Shahid 2013-02-16 13:02:56

+0

更改優先textiew顏色先生我無法理解這個概念sorrrry – Shahid 2013-02-16 13:09:10

+0

而不是使用SimpleCusrsorAdapter使用擴展CursorAdapter的cusom adpater – 2013-02-16 14:47:19

0

我會考慮使用一個BaseAdapter。這會讓你對每一行有更多的控制。

+0

是否有任何方法來根據文本 – Shahid 2013-02-16 12:51:02

+0

在列表視圖項目中設置textview顏色Yup,使用BaseAdapter可以更改顯示的視圖以及對視圖執行的操作。 – Ljdawson 2013-02-16 12:52:56

+0

先生從視圖我的意思SQlite數據庫視圖我從中獲取數據到列表視圖 – Shahid 2013-02-16 12:58:23