我有2个ListViews。一个在另一个上面当用户单击bottomList中的行时,我正在访问Web服务并创建新游标并重新填充bottomList。同时,我想获取单击的行并将其放入TopListView。我试图将一个Intent从cursorAdapter传递给MainActivity,但只是重新填充bottomListView,其值与那里相同。甚至不会调用Web服务。 TopListView没有任何反应。
是否可以将onClick的位置从bottomCursorAdapter传递给topCursorAdapter?或者像employee_number这样的变量,我可以在TopAdapter和swapCursor中查询它?如果是这样,不知道如何交换光标,因为没有办法判断是否有人点击了topListView中的bottomListView。
我的TopList方法
public void displayTopList() {
SQLiteDatabase db = dbHandler.getWritableDatabase();
mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
ListView mTopListView = (ListView) findViewById(R.id.mTopList);
TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, mTopCursor);
mTopListView.setAdapter(topAdapter);
}
我创建的方法是从适配器获取数据以显示toast
public void onRowClick(String mEmployeeNumber, String mFirstName) {
Toast.makeText(getApplicationContext(), "SEND TO TOP LIST " + mEmployeeNumber + " " + mFirstName, Toast.LENGTH_LONG).show();
}
我的TopAdapter,我正在尝试添加onRowClick。
public class TopListCursorAdapter extends CursorAdapter {
public TopListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
ViewHolder holder;
holder = new ViewHolder();
holder.tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
holder.tvLastName = (TextView) view.findViewById(R.id.personLastName);
holder.tvTitle = (TextView) view.findViewById(R.id.personTitle);
holder.mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
holder.mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
holder.mCardView = (CardView) view.findViewById(R.id.home_screen_cardView);
String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name"));
String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name"));
String mPayrollTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title"));
String mThumbnail = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData"));
holder.tvFirstName.setText(mFirstName);
holder.tvLastName.setText(mLastName);
holder.tvTitle.setText(mPayrollTitle);
if (mThumbnail != null) {
byte[] imageAsBytes = Base64.decode(mThumbnail.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
holder.mPeepPic.setImageBitmap(parsedImage);
} else {
holder.mPeepPic.setImageResource(R.drawable.img_place_holder_adapter);
}
final int position = cursor.getPosition();
holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cursor.moveToPosition(position);
String mEmployeeNumber = cursor.getString(1);
String mEmail = cursor.getString(8);
String mFirstName = cursor.getString(2);
String mLastName = cursor.getString(3);
String mPhoneMobile = cursor.getString(4);
String mPhoneOffice = cursor.getString(5);
String mCostCenter = cursor.getString(10);
String mHasDirectReports = cursor.getString(7);
String mTitle = cursor.getString(6);
String mPic = cursor.getString(9);
Intent mIntent = new Intent(context, EmployeeFullInfo.class);
mIntent.putExtra("Employee_number", mEmployeeNumber);
mIntent.putExtra("Email", mEmail);
mIntent.putExtra("First_name", mFirstName);
mIntent.putExtra("Last_name", mLastName);
mIntent.putExtra("Phone_mobile", mPhoneMobile);
mIntent.putExtra("Phone_office", mPhoneOffice);
mIntent.putExtra("Cost_center_id", mCostCenter);
mIntent.putExtra("Has_direct_reports", mHasDirectReports);
mIntent.putExtra("Payroll_title", mTitle);
mIntent.putExtra("ThumbnailData", mPic);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(mIntent);
}
});
}
public static class ViewHolder {
TextView tvFirstName;
TextView tvLastName;
TextView tvTitle;
ImageView mPeepPic;
ImageButton mDetailsButton;
CardView mCardView;
}
}
答案 0 :(得分:2)
如果我理解正确,您希望在一个适配器中发生单击事件时知道代码,然后将一些数据发送到另一个适配器。
在这种情况下,您可以简单地定义一个接口,该接口通过包含两者的Activity类来桥接适配器之间的“通信”。
例如,这是您的适配器定义了接口。单击按钮时会激活它。
friend
在Activity中,您可以实现该界面(也就是您的回调)
public class TopListCursorAdapter extends CursorAdapter {
public interface TopListClickListener {
void onTopListClick(int position); // Can add more parameters here
}
private TopListClickListener callback;
public TopListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
if (!(context instanceof TopListClickListener)) {
throw new ClassCastException("Context must implement TopListClickListener");
}
this.callback = (TopListClickListener) context;
}
...
holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cursor.moveToPosition(position);
...
if (callback != null) {
callback.onTopListClick(position); // Can pass more parameters here
}