设置listFragment中项目的颜色

时间:2013-03-07 10:14:24

标签: android

我正在使用简单光标适配器来填充 sherlockListFragment 中的列表。我已经使用loaders完成了。但是当我尝试为文本添加颜色时List.i我得到 Java.lang.Null.pointer exception.And我的java代码是

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.i("live", "onActivityCreated");
    super.onActivityCreated(savedInstanceState);

    setEmptyText("Loading...");

      SimpleCursorAdapter.ViewBinder vb=new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View arg0, Cursor arg1, int arg2) {
            Log.i("alls","inside set View Value");
            View inflate=getActivity().getLayoutInflater().inflate(R.layout.live_list_stock,null);
            TextView tv=(TextView)inflate.findViewById(R.id.lpercent);
            tv.setTextColor(Color.RED);
            //String i=tv.getText().toString();
            //Log.i("alls",(i));
            // TODO Auto-generated method stub
            return true;
        }
        };


    liveMyStocksadapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.live_list_item, null, new String[] {DBConstants.NAME,DBConstants.YSYMBOL,DBConstants.PRICE,DBConstants.PERCENT,DBConstants.DATE,DBConstants.TIME,DBConstants.OPEN,DBConstants.HIGH,DBConstants.LOW,DBConstants.VOLUME}, new int[] {R.id.lname,R.id.lysymbol, R.id.lprice,R.id.lpercent,R.id.ldate,R.id.ltime,R.id.lopen,R.id.lhigh,R.id.llow,R.id.lvolume},0);
    setListAdapter(liveMyStocksadapter);

    mycontext=this;
    getActivity().registerReceiver(FragmentReceiver1, new IntentFilter("fragmentupdater"));
    getActivity().getSupportLoaderManager().initLoader(0, null, this);
    liveMyStocksadapter.setViewBinder(vb);

感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

使用视图活页夹时,您无需夸大视图,SimpleCursorAdapter会为您完成。您所要做的就是设置值(并且只有当它们不是常见值,如图像,连接字符串等)时。注意:字符串,整数和默认的无格式值由默认视图绑定器设置。您只需要正确创建适配器。

实现ViewBinder时,作为新SimpleCursorAdapter参数插入的所有视图将作为视图值(arg0)的参数以及光标(右行中的arg1)和列(arg2)传递给您

尝试这样的事情:

public boolean setViewValue(View arg0, Cursor arg1, int arg2) {
   switch(arg0.getId()) {
   case R.id.lpercent:
      ((TextView)arg0).setTextColor(Color.RED);
      return false; // so it will set text view with DBConstants.PERCENT
   case ...: // TODO: put here some view you don't want default view binder set and then return true
      ((TextView)arg0).setText("non default value: " + arg1.getString(arg2));
   return true;
   }
}