0

私はsherlockListFragmentのリストを埋めるためにSimple Cursorアダプターを使用しています.iはローダーでそれを行いました.しかし、 List.iのテキストに色を追加しようとすると、Java.lang.Null.pointer例外が発生します.そして私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);

助けてくれてありがとう..

4

1 に答える 1

0

ビュー バインダーを使用する場合、ビューを拡張する必要はありません。SimpleCursorAdapter が自動的に実行します。必要なのは値を設定することだけです (画像や連結文字列などの一般的な値ではない場合のみ)。注: 文字列、int、およびデフォルトのフォーマットされていない値は、デフォルトのビュー バインダーによって設定されます。必要なのは、アダプタを適切に作成することだけです。

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;
   }
}
于 2013-03-08T05:18:09.987 に答える