我在一个列表视图中点击了一个员工的行,然后将其传递回MainActivity。然后我尝试将其传递给另一个将更新其ListView并添加用户单击的行的适配器。我正在向数据库添加员工(可能不是最好的,因为他们已经在数据库中有一行)。然后我创建一个光标,抓住该行并尝试将theCursor更改为topAdapter(我正在尝试更新的listView适配器)。
我在topAdapter.changeCursor(clcikedEmployee):
我不认为创建新的TopListCursorAdapter会起作用,因为我不想创建新的TopListCursorAdapter,只需更新并将新行添加到其中的现有列表中。我知道e不是null,因为当我将它打印到控制台时,我正确地获得了被点击用户的数据。只是无法将其传递给TopAdapter以将行添加到现有列表视图中。
是否可以传递给TopFragment并从那里更新TopListCursorAdapter以将新行添加到列表中?
主要活动
public class MainActivity extends FragmentActivity implements BottomListViewAdapter.BottomListClickListener {
private ProgressBar mProgressBar;
EmployeeDBHandler dbHandler;
private TopListCursorAdapter topAdapter;
private BottomListViewAdapter bottomAdapter;
private ArrayList<Employee> mEmployee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
dbHandler = new EmployeeDBHandler(getApplicationContext());
mProgressBar.setVisibility(View.VISIBLE);
getXMLData();
//GUI for seeing android SQLite Database in Chrome Dev Tools
Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this);
inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this));
Stetho.Initializer in = inBuilder.build();
Stetho.initialize(in);
}
public void getXMLData() {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder()
.url(getString(R.string.API_FULL_URL))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String responseData = response.body().string();
final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
for (Employee e : employees) {
dbHandler.addEmployee(e);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.GONE);
displayTopList();
displayBottomList();
}
});
}
});
}
public void displayTopList() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.topFragment, new TopFragment());
fragmentTransaction.commit();
}
public void displayBottomList() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.bottomFragment, new BottomFragment());
fragmentTransaction.commit();
}
@Override
public void onBottomListClick(Employee e) {
dbHandler.addEmployee(e);
Log.i("EMPLOYEE", e.toString());
TopFragment topFragment = (TopFragment) getSupportFragmentManager().findFragmentById(R.id.topFragment);
SQLiteDatabase db = dbHandler.getWritableDatabase();
final Cursor clickedEmployee = db.rawQuery("SELECT * FROM " + "employees" + " WHERE " +
"Employee_number" + "=" + e.getEmployee_number(), null);
// change the adapter's Cursor
topFragment.update(clickedEmployee);
}
}
TopFragment
public class TopFragment extends Fragment {
public Cursor mTopCursor;
EmployeeDBHandler dbHandler;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_top_list, container, false);
String table = "employees";
int mStartingEmployeeID = 3500075;
dbHandler = new EmployeeDBHandler(getContext());
SQLiteDatabase db = dbHandler.getWritableDatabase();
mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
ListView mTopListView = (ListView) view.findViewById(R.id.mTopList);
TopListCursorAdapter topAdapter = new TopListCursorAdapter(getContext(), mTopCursor);
mTopListView.setAdapter(topAdapter);
return view;
}
public void update(Cursor cursor) {
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{
mTopCursor, cursor });
}
}
底部碎片
public class BottomFragment extends Fragment {
public Cursor mBottomCursor;
EmployeeDBHandler dbHandler;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bottom_list, container, false);
dbHandler = new EmployeeDBHandler(getContext());
SQLiteDatabase db = dbHandler.getWritableDatabase();
String table = "employees";
int mStartingEmployeeID = 3500075;
mBottomCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " +
"Employee_number" + "!=" + mStartingEmployeeID + " AND " +
"Manager_employee_number" + "=" + mStartingEmployeeID + " ORDER BY " +
"Last_name" + " ASC", null);
ListView mBottomListView = (ListView) view.findViewById(android.R.id.list);
BottomListViewAdapter mBottomAdapter = new BottomListViewAdapter(getContext(), mBottomCursor);
mBottomListView.setAdapter(mBottomAdapter);
return view;
}
}
答案 0 :(得分:0)
除非员工可以共享ID,否则此代码只会让您在ListView中显示一个员工。 (在活动中)
dbHandler.addEmployee(e); TopFragment topFragment = (TopFragment) getSupportFragmentManager().findFragmentById(R.id.topFragment); SQLiteDatabase db = dbHandler.getWritableDatabase(); final Cursor clickedEmployee = db.rawQuery("SELECT * FROM " + "employees" + " WHERE " + "Employee_number" + "=" + e.getEmployee_number(), null); // change the adapter's Cursor topFragment.update(clickedEmployee);
此代码也将如此(在TopFragment中)
int mStartingEmployeeID = 3500075; dbHandler = new EmployeeDBHandler(getContext()); SQLiteDatabase db = dbHandler.getWritableDatabase(); mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null); ListView mTopListView = (ListView) view.findViewById(R.id.mTopList); TopListCursorAdapter topAdapter = new TopListCursorAdapter(getContext(), mTopCursor); mTopListView.setAdapter(topAdapter);
您正在尝试向适配器添加行,而不是完全替换它。所以,要做到这一点,我认为你需要在你的任何数据交换混乱之前将INSERT
语句放入数据库,然后重新编写查询,以便所有结果你正在尝试插入将返回。
例如,如果ID自动递增,您可能需要WHERE Employee_number >= " + mStartingEmployeeID
。
此外,这可能无法解决某些问题,您违反了dbHandler
实例的目的,即&#34;封装&#34;你的疑问。
所以,而不是这个
SQLiteDatabase db = dbHandler.getWritableDatabase();
mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
推荐方式只是
topCursor = dbHandler.getEmployeeWithId(mStartingEmployeeID);
如果您已经有类似的方法返回Employee
,那么您可以改为使用Employee(Cursor c)
构造函数,而不是像这样使用
Employee e = new Employee(dbHandler.getEmployeeWithId(mStartingEmployeeID));
答案 1 :(得分:0)
所以我能够让它最终发挥作用。我所做的是从bottomList获取光标,将其与TopLists现有光标合并,然后将该光标传递给TopAdapter。
public void update(Cursor cursor) {
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{cursor, mTopCursor});
TopListCursorAdapter topAdapter = new TopListCursorAdapter(getContext(), mergeCursor);
mTopListView.setAdapter(topAdapter);
}