用于显示数据库中下一列的按钮

时间:2014-05-15 15:38:49

标签: android sqlite button

任何人都可以向我解释为什么我按下它时屏幕没有显示我数据库中的下一个结果?我知道按钮被拿起来,只是无法弄清楚它为什么不做任何事情。

public static class PlaceholderFragment extends Fragment 
    {

        public PlaceholderFragment() 
        {
        }
        Cursor c;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {
            View rootView = inflater.inflate(
                    R.layout.fragment_resultsand_statistics, container, false);


            Button next = (Button)rootView.findViewById(R.id.next_result);
            next.setOnClickListener(new View.OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    c.moveToNext();
                }
            });

            SQLController db = new SQLController(getActivity());
            TextView PA = (TextView)rootView.findViewById(R.id.PlayerA);
            TextView PB = (TextView)rootView.findViewById(R.id.PlayerB);
            TextView PAS = (TextView)rootView.findViewById(R.id.PlayerAS);
            TextView PBS = (TextView)rootView.findViewById(R.id.PlayerBS);
            db.open();

            c = db.readData();
            c.moveToFirst();
            String s = c.getString(c.getColumnIndex("playerA_name"));
            PA.setText(s);
            s = c.getString(c.getColumnIndex("playerB_name"));
            PB.setText(s);
            s = c.getString(c.getColumnIndex("playerA_score"));
            PAS.setText(s);
            s = c.getString(c.getColumnIndex("playerB_score"));
            PBS.setText(s);
            return rootView;
        }
    }

1 个答案:

答案 0 :(得分:0)

似乎您已更新光标,但忘记更新视图!试试这样:

public static class PlaceholderFragment extends Fragment 
{

    public PlaceholderFragment() 
    {
    }

    public void updateViews(Cursor c){
        String s = c.getString(c.getColumnIndex("playerA_name"));
        PA.setText(s);
        s = c.getString(c.getColumnIndex("playerB_name"));
        PB.setText(s);
        s = c.getString(c.getColumnIndex("playerA_score"));
        PAS.setText(s);
        s = c.getString(c.getColumnIndex("playerB_score"));
        PBS.setText(s);
        return rootView;

    }


    SQLController db;
    TextView PA;
    TextView PB ;
    TextView PAS;
    TextView PBS;

    Cursor c;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View rootView = inflater.inflate(
                R.layout.fragment_resultsand_statistics, container, false);


        Button next = (Button)rootView.findViewById(R.id.next_result);
        next.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                c.moveToNext();
                updateViews(c);
            }
        });

        db = new SQLController(getActivity());
        PA = (TextView)rootView.findViewById(R.id.PlayerA);
        PB = (TextView)rootView.findViewById(R.id.PlayerB);
        PAS = (TextView)rootView.findViewById(R.id.PlayerAS);
        PBS = (TextView)rootView.findViewById(R.id.PlayerBS);
        db.open();

        c = db.readData();
        c.moveToFirst();
        updateViews(c)


    }
}