2013-04-10 88 views
1

我不太确定这是否是正确的标题。但是,我需要做的是向我的getChildrenCursor添加某种参数,以便它仅显示该特定组的子项,而不显示每个组的所有子项。按照getExtras的工作方式排序。如何将groupCursor参数移动到childCursor?

我正在使用扩展SimpleCursorTreeAdapter的Fragments和ExpandabeListAdaptor。正如我所提到的,除了所有的孩子都为每个小组加载之外,一切都很完美。我相信我必须将每个组的ID映射给它的孩子,但我不确定如何做到这一点。

我已经看到了一堆其他代码示例中的这一点,但我不知道如何为我的代码植入它。这样的事情我相信:

Cursor childCursor = mDbHelper.fetchChildren(GroceryListMain.group, 
      groupCursor.getString(groupCursor.getColumnIndex("_id")); 

这是我认为我需要做的想法,但我不知道。 我只使用一个数据库表。

我的代码项目添加到ExpandableListiew是这样的:

package com.example.fragments; 

import android.content.Context; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.ExpandableListView; 
import android.widget.FilterQueryProvider; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.SimpleCursorTreeAdapter; 
import android.widget.SimpleExpandableListAdapter; 

import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.app.SherlockListFragment; 




public class JobOpportunities extends SherlockFragment{ 

private JobDbAdaptor dbHelper; 
private MyExpandableListAdapter dataAdapter; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    return inflater.inflate(R.layout.jobmain, container, false); 

} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 

    super.onActivityCreated(savedInstanceState); 

     dbHelper = new JobDbAdaptor(getActivity().getApplicationContext()); 
     dbHelper.open(); 

     //Clean all data 
     dbHelper.deleteAllJobs(); 
     //Add some data 
     dbHelper.insertSomeJobs(); 


     //Generate ListView from SQLite Database 
    displayListView(); 




}//end of activity create 

private void displayListView() 
{ 
    Cursor cursor = dbHelper.fetchAllJobs(); 

    ExpandableListView explist = (ExpandableListView) getView().findViewById(R.id.expandList); 

    dataAdapter = new MyExpandableListAdapter(cursor, getActivity().getApplicationContext(), 
      R.layout.job_info, 
      R.layout.child_info, 
      new String[] {JobDbAdaptor.KEY_NAME}, 
      new int[] {R.id.name}, 
      new String[] {JobDbAdaptor.KEY_DESCRIPTION, JobDbAdaptor.KEY_POSITION, JobDbAdaptor.KEY_LOCATION}, 
      new int[] {R.id.childitem1, R.id.childitem2, R.id.childitem3}); 

    explist.setAdapter(dataAdapter); 

}//end of list view 

    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter 
    { 
     public MyExpandableListAdapter(Cursor cursor, Context jobOpportunities, int groupLayout, 
       int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom, 
       int[] childrenTo) { 
      super(jobOpportunities, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom, 
        childrenTo); 
     } 

     @Override 
     protected Cursor getChildrenCursor(Cursor fetchGroup) 
     { 
      // Given the group, we return a cursor for all the children within that group 


      fetchGroup = dbHelper.fetchchildren(); 

      return fetchGroup; 

     }//end of childcursor 

    }//end of myexpandable list adapter 


}//end of jobopportunities 

,这是我的数据库代码:

package com.example.fragments; 

    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.SQLException; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.util.Log; 

    public class JobDbAdaptor { 

    //field names 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_NAME = "name"; 
    public static final String KEY_DESCRIPTION = "description"; 
    public static final String KEY_POSITION = "postition"; 
    public static final String KEY_LOCATION = "location"; 

    //variables 
    private static final String TAG = "JobDbAdapter"; 
    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

//database info 
private static final String DATABASE_NAME = "Opportunities"; 
private static final String SQLITE_TABLE = "Job_Info"; 
private static final int DATABASE_VERSION = 1; 

private final Context mCtx; 

//defines the fields and constraints 
private static final String DATABASE_CREATE = 
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" + 
KEY_ROWID + " integer PRIMARY KEY autoincrement," + 
KEY_NAME + "," + 
KEY_DESCRIPTION + "," + 
KEY_POSITION + "," + 
KEY_LOCATION + ");"; 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 

//constructor for database sets name and version 
DatabaseHelper(Context context) 
{ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 


@Override//creates database 
public void onCreate(SQLiteDatabase db) 
{ 
    Log.w(TAG, DATABASE_CREATE); 
    db.execSQL(DATABASE_CREATE); 
} 

@Override//checks for new version and overrides old 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
    + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE); 
    onCreate(db); 
} 
}//end of update 

public JobDbAdaptor(Context ctx) 
{ 
this.mCtx = ctx; 
} 

//opens the database and sets the state 
public JobDbAdaptor open() throws SQLException 
{ 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase();//state i.e. writable or readable 
    return this; 
    } 

    //closes database 
    public void close() 
    { 

    if (mDbHelper != null) 
    { 
    mDbHelper.close(); 
    } 

    } 

    //creates each job 
    public long createJob(String name, String description, 
    String position, String location) 
    { 

    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_NAME, name); 
    initialValues.put(KEY_DESCRIPTION, description); 
    initialValues.put(KEY_POSITION, position); 
    initialValues.put(KEY_LOCATION, location); 

    return mDb.insert(SQLITE_TABLE, null, initialValues); 

    }//end of create jobs 

    //deletes all of the jobs 
    public boolean deleteAllJobs() 
    { 

    int doneDelete = 0; 
    doneDelete = mDb.delete(SQLITE_TABLE, null , null); 
    Log.w(TAG, Integer.toString(doneDelete)); 
    return doneDelete > 0; 

    }//end of delete all jobs 

    //searches the job based on input text 
    public Cursor fetchJobsByName(String inputText) throws SQLException 
    { 
    Log.w(TAG, inputText); 
    Cursor mCursor = null; 
    if (inputText == null || inputText.length() == 0) { 
    mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_NAME, KEY_DESCRIPTION, KEY_POSITION, KEY_LOCATION}, 
    null, null, null, null, null); 

    } 
    else 
    { 
    mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_NAME, KEY_DESCRIPTION, KEY_POSITION, KEY_LOCATION}, 
    KEY_NAME + " like '%" + inputText + "%'", null, 
    null, null, null, null); 
    } 
    if (mCursor != null) 
    { 
    mCursor.moveToFirst(); 
    } 

    return mCursor; 

    }//end of fetchjobs by name 

    //gets all of the jobs 
    public Cursor fetchAllJobs() 
    { 

    Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_NAME, KEY_DESCRIPTION, KEY_POSITION, KEY_LOCATION}, 
    null, null, null, null, null); 

    if (mCursor != null) { 
    mCursor.moveToFirst(); 
    } 
    return mCursor; 

    }//end of fetch all jobs 

    //fetches the group headers or just the titles of the job 
    public Cursor fetchgroup() 
    { 
    Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, null, 
    null, null, null, null); 

    if(mCursor != null) 
{ 
    mCursor.moveToFirst(); 

} 

return mCursor; 

    }//end of fetchgroups 

    //fetches the the info for the children 
    public Cursor fetchchildren() 
    { 
    Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_DESCRIPTION, KEY_POSITION, KEY_LOCATION}, null, 
    null, null, null, null); 

if(mCursor != null) 
{ 
    mCursor.moveToFirst(); 

} 

return mCursor; 

    }//end of fetchchildren 

    //holds the content for each job 
    public void insertSomeJobs() 
    { 

    createJob("AFG","Afghanistan","Asia","Southern and Central Asia"); 
    createJob("ALB","Albania","Europe","Southern Europe"); 
    createJob("DZA","Algeria","Africa","Northern Africa"); 
    createJob("ASM","American Samoa","Oceania","Polynesia"); 
    createJob("AND","Andorra","Europe","Southern Europe"); 
    createJob("AGO","Angola","Africa","Central Africa"); 
    createJob("AIA","Anguilla","North America","Caribbean"); 

    }//end of insertjobs 

    }//end of JobDbAdaptor 

我知道这是愚蠢的东西,我只是没有得到。如果任何人都能指引我走向正确的方向,那会很棒。

谢谢你的时间和帮助。

回答

0

大声笑我知道这是愚蠢的。

这是新getChildrenCursor:

@Override 
     protected Cursor getChildrenCursor(Cursor fetchGroup) 
     { 
      // Given the group, we return a cursor for all the children within that group 

      String dbcolumnId = JobDbAdaptor.KEY_NAME; 
      Cursor fetchchild=dbHelper.fetchchildren(fetchGroup.getString(fetchGroup.getColumnIndex(dbcolumnId))); 

      return fetchchild; 


     }//end of childcursor 

所以我不得不将它设置为读取来自组,我知道我必须,但不知道怎么的价值。这根据小组设置孩子,但它只是将它从一个表中拉出来。所以当点击时,该组只显示它应该的孩子。

这里是在数据库中的光标的方法,它是modifidy到Accpet头的新值:

public Cursor fetchchildren(String dbcolumnId) 
{ 
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_DESCRIPTION, KEY_POSITION, KEY_LOCATION}, KEY_NAME + "='" + dbcolumnId + "'", 
    null, null, null, null); 

if(mCursor != null) 
{ 
    mCursor.moveToFirst(); 

} 

return mCursor; 

}//end of fetchchildren 

这不会阻止SQL注入,但我不是在这里具有使用输入任何东西。