1

我不确定这是否是正确的标题。但是,我需要做的是向我的 getChildrenCursor 添加某种参数,以便它只显示该特定组的孩子,而不是每个组的所有孩子。排序类似于 getExtras 的工作方式。

我正在使用片段和扩展 SimpleCursorTreeAdapter 的 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

我知道这是我没有得到的愚蠢的东西。如果有人能指出我正确的方向,那就太好了。

感谢您的时间和帮助。

4

1 回答 1

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

所以我不得不将它设置为从组中读取值,我知道我必须这样做,但不知道如何去做。这会根据组设置孩子,但它只会将其拉出一张桌子。因此,当单击该组时,它只会显示它应该显示的孩子。

这是数据库中的 Cursor 方法,接受新值是经过修改的:

 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 注入,但我在这里没有使用任何输入。

于 2013-04-11T15:08:55.013 回答