2013-03-07 55 views
0

我使用简单的游标适配器填写sherlockListFragment名单。我这样做了与loaders.But当我试图颜色在列表中添加到文本。我是越来越Java.lang.Null.pointer exception.And我的Java代码颜色设置到该项目中listFragment

@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); 

感谢您的帮助..

+0

你可以把堆栈跟踪 – DjHacktorReborn 2013-03-07 10:16:20

回答

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; 
    } 
}