それが正しいタイトルかどうかはよくわかりません。ただし、getChildrenCursor にある種のパラメーターを追加して、各グループのすべての子ではなく、その特定のグループの子のみを表示する必要があります。getExtras の仕組みと同じように並べ替えます。
Fragments と、SimpleCursorTreeAdapter を拡張する ExpandabeListAdaptor を使用しています。前述したように、すべての子が各グループにロードされることを除いて、すべてが完全に機能しています。各グループの ID をその子にマップする必要があると思いますが、これを行う方法がわかりません。
これは他の多くのコード サンプルで見たことがありますが、自分のコードに実装する方法がわかりません。私が信じているこのようなもの:
Cursor childCursor = mDbHelper.fetchChildren(GroceryListMain.group,
groupCursor.getString(groupCursor.getColumnIndex("_id"));
それが私がする必要があると思うことのアイデアですが、よくわかりません。データベース テーブルを 1 つだけ使用しています。
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
私が得られないのは愚かなことだと私は知っています。誰かが私を正しい方向に向けることができれば、それは素晴らしいことです.
お時間をいただき、ありがとうございました。