如何使用sqlite数据填充listview,这些数据是水平滑动的

时间:2012-05-03 17:51:21

标签: android sqlite android-viewpager

我的目标是水平滑动列表,由公共类填充ClientsManagerOpenHandler扩展SQLiteOpenHelper。 我尝试通过使用viewpager和ListFragments来实现这一目标。如果您有其他解决方案,请告诉我。

现在问题: 如果我尝试从以下PageListFragment.java调用,来自ClientsManagerOpenHandler的数据程序崩溃在:

 dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht"); 

也许我不能在ListFragment中调用扩展的SQLiteOpenHelper?但是我如何从sqlite中获取数据到我的列表中,以及当我水平滑动以更改数据时...

请帮忙。我尝试过任何事情,但我现在真的需要帮助。

public class PageListFragment extends ListFragment implements OnClickListener,
    LoaderCallbacks<Cursor> {

private Calendar cal = Calendar.getInstance();
private ClientsManagerOpenHandler openHandler;
public static final String PREFS_NAME ="MyPrefsFile";
SharedPreferences prefs;`

private Cursor dbCursorTerminAnsicht;
private Integer intVerdienst = 0;
private String queryVerdienst;
private SimpleCursorAdapter mCursorAdapter;
private ListView listViewTermine;
private final int listNr;
private final String[] fruit = { "Bananen", "Apfle", "Erdbeere",
        "Kirschen", "Mangos" };
private Uri[] mMediaSource = {null, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI};

public PageListFragment(int nr) {
    this.listNr = nr;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


//!!!!! after the next line the program crashes. Even I have set breakpoints at ClientsManagerOpenHandler 
//I cannot see anything in the debugger (Debugger: Source not found...) 


    dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht"); 

    if (listNr == 0) {
        ArrayAdapter<String> openHandler = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, fruit);
        setListAdapter(openHandler);
    } else if (listNr == 1) {
        mCursorAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1, null,
                new String[] { MediaStore.Audio.Artists.ARTIST },
                new int[] { android.R.id.text1 }, 0);
        setListAdapter(mCursorAdapter);
        getLoaderManager().initLoader(0, null, this);
    } else if (listNr == 2) {

        openHandler = new ClientsManagerOpenHandler(getActivity());
         String query = "projekte, klienten, termine WHERE termine.KLIENTID = klienten._id AND termine.PROJEKTID = projekte._id ORDER BY BEGINN ASC;";



        MyDataAdapter myClientsadapter = new MyDataAdapter (
                getActivity(),
                  R.layout.terminzeile,
                 // android.R.layout.two_line_list_item,
                  dbCursorTerminAnsicht,
                 new String[] { openHandler.BEGINN , openHandler.ENDE, openHandler.NACHNAME, openHandler.VORNAME, openHandler.PROJEKT, openHandler.BEZAHLT},
                 // fields,
                //  new int[] {R.id.editTextNachname, R.id.editTextVorname }
                  new int[] {R.id.textViewBeginn, R.id.textViewEnde, R.id.textViewNachname, R.id.textViewVorname, R.id.textViewProjekt,R.id.checkBoxBezahlt }
                  );


        myClientsadapter.setViewBinder(new MyDataAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {


                if(columnIndex == 13) {
                        String strBeginn = cursor.getString(columnIndex);
                        CheckBox cb = (CheckBox) view;
                        int intbezahlt = cursor.getInt(13);
                        int index = cursor.getColumnIndex("SATZ");
                        Integer intSatz = cursor.getInt(index);
                        if (index>0) {
                            if (intbezahlt>0){
                                intVerdienst = intVerdienst + intSatz;
                            }
                        }
                        cb.setChecked(intbezahlt > 0);
                        return true;
                }

                String str = cursor.getString(columnIndex);
                return false;
            }
        });

        //TerminlisteRefresh("");

        setListAdapter(myClientsadapter);
        getLoaderManager().initLoader(0, null, this);
    }
}


public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    Loader<Cursor> loader = new CursorLoader(getActivity(), mMediaSource[listNr],
            null, null, null, null);
    return loader;
}


public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
    mCursorAdapter.swapCursor(cursor);

}


public void onLoaderReset(Loader<Cursor> arg0) {
    mCursorAdapter.swapCursor(null);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}





}

1 个答案:

答案 0 :(得分:0)

您似乎在定义之前尝试访问openHandler

dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");
...
openHandler = new ClientsManagerOpenHandler(getActivity());

这可能会更好:

openHandler = new ClientsManagerOpenHandler(getActivity());
dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");