在列表视图中动态更改textview的颜色

时间:2013-04-02 05:26:26

标签: android listview textview

I have this list view here>I have shown certain amounts and now I want that the amounts with a - to be of red color and amounts with a + to be of green color

我在这里有这个列表视图。我已经显示了一定数量,现在我想要的是 - 的颜色为 - 红色,数量为+为绿色。

这是我的代码 - :

public class Myperwallet extends ListActivity {

    ImageView upload,more,logout,send,history;
    String userpin;
    TextView fname,lname,cur,bal,pin;
    String firstname,lastname,curr,balance,pin1,deb;
    static final String baseURL = "http://192.168.0.2:10095/androidxml/permywallet.php?usenam=";


    static final String KEY_ITEM = "item"; 
    static final String KEY_DATE = "date";
    static final String KEY_DEB = "debit";
    static final String KEY_TID = "transactionID";
    static final String KEY_AMO = "amount";
    static final String KEY_CAT = "category";
    static final String KEY_FNM = "firstname";
    static final String KEY_LNM = "lastname";
    static final String KEY_PIN = "pin";
    static final String KEY_BAL = "balance";
    static final String KEY_CUR = "currency";
    static final String KEY_FEES = "fees";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myperwallet);

        Bundle gotuser= getIntent().getExtras();
        userpin= gotuser.getString("username");

         more = (ImageView)findViewById(R.id.imageView6);
         upload= (ImageView)findViewById(R.id.imageView3);
         logout =(ImageView)findViewById(R.id.logout);
         send =(ImageView)findViewById(R.id.imageView4);
         history =(ImageView)findViewById(R.id.imageView5);
         fname = (TextView)findViewById(R.id.textView1);
         lname = (TextView)findViewById(R.id.textView2);
         cur = (TextView)findViewById(R.id.textView5);
         bal = (TextView)findViewById(R.id.textView4);
         pin = (TextView)findViewById(R.id.textView6);

         history.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String username2 = userpin;
                Bundle user1 = new Bundle();
                user1.putString("username",username2); 

                Intent next = new Intent(Myperwallet.this,History.class);
                next.putExtras(user1);
                startActivity(next);
            }
         });

         more.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String username2 = userpin;
                Bundle user1 = new Bundle();
                user1.putString("username",username2); 

                Intent next = new Intent(Myperwallet.this,More.class);
                next.putExtras(user1);
                startActivity(next);
            }
        });
         upload.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String username2 = userpin;
                    Bundle user1 = new Bundle();
                    user1.putString("username",username2); 

                    Intent next = new Intent(Myperwallet.this,Uploadfund.class);
                    next.putExtras(user1);
                    startActivity(next);
            }
          });

         send.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String username2 = userpin;
                Bundle user1 = new Bundle();
                user1.putString("username",username2); 

                Intent i = new Intent(Myperwallet.this,Sendfund.class);
                i.putExtras(user1);
                startActivity(i);
            }
         });

         logout.setOnClickListener(new OnClickListener() {   
              public void onClick(View v) {

                AlertDialog alertDialog = new AlertDialog.Builder(Myperwallet.this).create(); 
                alertDialog.setTitle("FastCashier:");
                alertDialog.setMessage("Are you sure you want to exit?");
                alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {

                       Intent logout = new Intent(Myperwallet.this,Login.class);
                       startActivity(logout);

                   }
                   });

                alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener()    {
                  public void onClick(DialogInterface dialog, int which) {

                      dialog.cancel();

                  }
                  });

                alertDialog.show();  
              }
            });

         StringBuilder URL= new StringBuilder(baseURL);
         URL.append(userpin);
         String fullUrl =URL.toString();
         Log.i("log_tag", "new url  " + fullUrl);

         ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        inboxParser parser = new inboxParser();
        String xml = parser.getXmlFromUrl(fullUrl); 
        Document doc = parser.getDomElement(xml); 



        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        for (int i = 0; i < nl.getLength(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            firstname = parser.getValue(e, KEY_FNM);
            lastname = parser.getValue(e, KEY_LNM);
            curr = parser.getValue(e, KEY_CUR);
            balance= parser.getValue(e, KEY_BAL);
            pin1= parser.getValue(e, KEY_PIN);
            deb = parser.getValue(e, KEY_DEB);

            map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
            map.put(KEY_DEB, parser.getValue(e, KEY_DEB));
            map.put(KEY_TID, parser.getValue(e, KEY_TID));
            map.put(KEY_AMO, parser.getValue(e, KEY_AMO));
            map.put(KEY_CAT, parser.getValue(e, KEY_CAT));
            map.put(KEY_FNM, parser.getValue(e, KEY_FNM));
            map.put(KEY_LNM, parser.getValue(e, KEY_LNM));
            map.put(KEY_CUR, parser.getValue(e, KEY_CUR));
            map.put(KEY_PIN, parser.getValue(e, KEY_PIN));
            map.put(KEY_BAL, parser.getValue(e, KEY_BAL));
            map.put(KEY_FEES, parser.getValue(e, KEY_FEES));


            menuItems.add(map);
        }
        fname.setText(firstname);
        lname.setText(lastname);
        cur.setText(curr);
        bal.setText(balance);
        pin.setText(pin1);



        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.recentlist,            
                new String[] { KEY_CAT, KEY_DATE, KEY_TID, KEY_AMO, KEY_DEB,KEY_CUR,KEY_BAL,KEY_FEES}, new int[] {
                R.id.textView1,R.id.textView2, R.id.textView3, R.id.textView4,R.id.textView5,R.id.textView7,R.id.textView6,R.id.textView8});


        setListAdapter(adapter);


       }
}

xml文件的代码 - :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/mywallet" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/wallet_ic_hover"
        android:tileMode="repeat" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView2"
        android:background="@drawable/upload_ic"
        android:tileMode="repeat" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView3"
        android:background="@drawable/send_ic"
        android:tileMode="repeat" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView4"
        android:background="@drawable/history1"
        android:tileMode="repeat" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView5"
        android:background="@drawable/more"
        android:tileMode="repeat" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="84dp"
        android:background="@drawable/recent_activity"
        android:tileMode="repeat" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView7" >
    </ListView>

    <ImageView
        android:id="@+id/logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="7dp"
        android:src="@drawable/logout" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView3"
        android:layout_below="@+id/imageView1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:textSize="9pt"
        android:gravity="right"
        android:textStyle="bold"
        android:textColor="#2e508b"
        android:text="TextV" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:layout_marginLeft="10dp"
        android:textSize="9pt"
        android:gravity="left"
        android:textStyle="bold"
        android:textColor="#2e508b"
        android:text="Text" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:textStyle="bold"
        android:textColor="#2e508b"
        android:textSize="8pt"
        android:layout_marginLeft="5dp"
        android:text="Balance:" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:layout_marginLeft="5dp"
        android:textColor="#2e508b"
        android:textSize="8pt"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toRightOf="@+id/textView4"
        android:layout_marginLeft="5dp"
         android:textColor="#2e508b"
         android:textSize="8pt"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignParentRight="true"
        android:textColor="#2e508b"
        android:textSize="8pt"
        android:layout_marginRight="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView6"
        android:layout_alignBottom="@+id/textView6"
        android:layout_toLeftOf="@+id/textView6"
        android:textStyle="bold"
        android:textColor="#2e508b"
        android:layout_marginRight="5dp"
        android:textSize="8pt"
        android:text="Pin:" />

</RelativeLayout>

这是listview的xml布局。

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:layout_marginLeft="5dp"
        android:textStyle="bold"
        android:textColor="#2E508B"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:textColor="#2E508B"
        android:layout_marginRight="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="10dp"
        android:textColor="#2E508B"
        android:layout_marginLeft="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:text="Medium Text"
        android:textColor="#2e508b"
        android:textStyle="bold"
        android:layout_marginRight="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toLeftOf="@+id/textView4"
        android:text="T"
        android:textStyle="bold"
        android:textColor="#2e508b"
        android:layout_marginRight="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="19dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" 
        android:visibility="gone"
        />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:text="TextView" 
        android:visibility="gone"
        />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:text="TextView" 
        android:visibility="gone"
        />

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

这是非常简单的技巧,没有html。

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable WordtoSpan = new SpannableString("Your A amount");        

WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, WordtoSpan .length(),             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(WordtoSpan);

答案 1 :(得分:0)

根据您的要求创建一个自定义适配器,在getView()方法中,您可以更改textview颜色,具体取决于您的条件。看看这个定制适配器 http://www.ezzylearning.com/tutorial.aspx?tid=1763429

答案 2 :(得分:0)

您必须创建自定义适配器,并且在getView()中,您可以根据需要更改颜色

 MyArrayAdapter adapter = new MyArrayAdapter(this, listArr);
 listView.setAdapter(adapter);

public MyArrayAdapter(Activity context, String[] objects) {
            super(context, R.layout.message, objects);
            // TODO Auto-generated constructor stub
            this.context = context;
            listArr = objects;

        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub



            LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.message, null, true);

            TextView textView = (TextView) view.findViewById(R.id.txtMessage);
                textView.setText(Html.fromHtml(listArr[position]));
                textView.setTextColor(getResources().getColor(R.color.red));

            return view;
        }
    }

}

答案 3 :(得分:0)

要在itemView(或ListView)的创建时自定义ListActivity,您必须创建自己的 View Adapter ,而不是使用SimpleAdapter

public class WalletAdapter extends ArrayAdapter<BillData>
{
    BillData[] mData;
    int mLayoutResourceId;
    public WalletAdapter (Context context, int layoutResourceId, BillData[] data) 
    {
            super(context, layoutResourceId, data);
            this.mLayoutResourceId= layoutResourceId;
            this.mData = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // inflate your item view from resource if need
        // get your textview in the item, check its content and modify it

        // the following is sample code
        LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
        row = inflater.inflate(mLayoutResourceId, parent, false);

        TextView walletItem1 = (TextView)row.findViewById(R.id.txt1);
        if(your_condition_of_text_content)
        {
            walletItem1.setColor (your_color);//or set background color or text color
        }
    }
}

然后在活动 onCreate() 中,您使用附加的帐单数据数组初始化 WalletAdapter ,然后 setListAdapter(your_instance_of_wallet_adapter)

注意:您可以使用ArrayList或任何其他集合类型更改适配器,以便使用动态数据列表以提高效率。