动态设置textview的颜色

时间:2012-03-17 13:35:59

标签: android

我通过以下代码使用自定义列表视图

public class details extends ListActivity {
    /** Called when the activity is first created. */

    Bundle extras;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_list_view);
         extras=getIntent().getExtras();

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                R.layout.custom_row_view,
                new String[] {"name","use"},
                new int[] {R.id.text1,R.id.text2}
                );
        populateList();
        setListAdapter(adapter);
    }

    static final ArrayList<HashMap<String,String>> list = 
        new ArrayList<HashMap<String,String>>(); 

    private void populateList() 
    {

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("name","NAME");
        temp1.put("use",extras.getString("name"));
        list.add(temp1);
        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("name","CHANGE IN PRICE");
        temp2.put("use",extras.getString("change"));
            TextView txt=(TextView)findViewById(R.id.text2);
double k=Double.parseDouble(extras.getString("change"));
        if(k<0)
        {
            txt.setTextColor(Color.RED);
        }
        else
        {
            txt.setTextColor(Color.RED);
        }
        list.add(temp2);


    }





}

如果价格变化大于0,我想将textview的颜色设置为绿色,否则我希望它的颜色为红色。以下代码在setTextColor()处抛出空指针异常。我到底是怎么做的

0 个答案:

没有答案
相关问题