gpt4 book ai didi

android - 从适配器隐藏数据库行

转载 作者:行者123 更新时间:2023-11-30 00:45:05 25 4
gpt4 key购买 nike

我有一个通过网络服务调用填充的数据库。我想知道如何仅从 ListView 中隐藏其中一行?我不想删除记录只是隐藏它所以它不会在 ListView 中显示该行,因为我正在使用该行填充上面的另一个 ListView。有没有办法在像 employeeNumber 这样的列中隐藏我的特定值?

我正在用游标填充 ListView,并希望有一种方法可以仅隐藏第 1 行,因为我不希望它显示在我的 ListView 中。

public void displayBottomList() {
SQLiteDatabase db = dbHandler.getWritableDatabase();
Cursor mBottomListCursor = db.rawQuery("SELECT * FROM employees", null);


ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
BottomListViewAdapter bottomAdapter = new BottomListViewAdapter(this, mBottomListCursor);
mBottomListView.setAdapter(bottomAdapter);
}

不太确定如何使用数据库中的 Employee_number 列在此方法中隐藏这一行。

public class BottomListViewAdapter extends CursorAdapter {
int position;

public BottomListViewAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {
final int pos = cursor.getPosition();
final ViewHolder viewHolder = (ViewHolder) view.getTag();

String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name"));
String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name"));
String mTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title"));
String mThumbnail = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData"));

viewHolder.tvFirstName.setText(mFirstName);
viewHolder.tvLastName.setText(mLastName);
viewHolder.tvTitle.setText(mTitle);

if(mThumbnail != null) {
byte[] imageAsBytes = Base64.decode(mThumbnail.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
viewHolder.mPeepPic.setImageBitmap(parsedImage);
} else {
viewHolder.mPeepPic.setImageResource(R.drawable.img_place_holder_adapter);
}

viewHolder.mDetailsButton.setTag(cursor.getPosition());
viewHolder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.getTag();
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(view.getContext(), 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);
view.getContext().startActivity(mIntent);
}
});
}/*

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from()
}
}*/

public static class ViewHolder {
TextView tvFirstName;
TextView tvLastName;
TextView tvTitle;
ImageView mPeepPic;
ImageButton mDetailsButton;
CardView mCardView;

public ViewHolder(View view) {
tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
tvLastName = (TextView) view.findViewById(R.id.personLastName);
tvTitle = (TextView) view.findViewById(R.id.personTitle);
mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
}
}

/*@NonNull
@Override
public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
final Employee employee = getItem(position);
final AppViewHolder holder;

convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_cardview_layout, parent, false);
holder = new AppViewHolder();

holder.tvFirstName = (TextView) convertView.findViewById(R.id.personFirstName);
holder.tvLastName = (TextView) convertView.findViewById(R.id.personLastName);
holder.tvTitle = (TextView) convertView.findViewById(R.id.personTitle);
holder.mPeepPic = (ImageView) convertView.findViewById(R.id.person_photo);
holder.mDetailsButton = (ImageButton) convertView.findViewById(R.id.fullDetailButton);
holder.mCardView = (CardView) convertView.findViewById(R.id.home_screen_cardView);

if (employee != null) {
holder.tvFirstName.setText(employee.getFirst_name());
holder.tvLastName.setText(employee.getLast_name());
holder.tvTitle.setText(employee.getPayroll_title());

if (employee.getThumbnailData() != null) {
String peepPicData = employee.getThumbnailData();
byte[] imageAsBytes = Base64.decode(peepPicData.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
holder.mPeepPic.setImageBitmap(parsedImage);
}

holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mEmployeeNumber = employee.getEmployee_number();
String mEmail = employee.getEmail();
String mFirstName = employee.getFirst_name();
String mLastName = employee.getLast_name();
String mPhoneMobile = employee.getPhone_mobile();
String mPhoneOffice = employee.getPhone_office();
String mCostCenter = employee.getCost_center_id();
String mHasDirectReports = employee.getHas_direct_reports();
String mTitle = employee.getPayroll_title();
String mPic = employee.getThumbnailData();
Intent mIntent = new Intent(view.getContext(), 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);
view.getContext().startActivity(mIntent);
}
});

holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mEmployeeNumber = employee.getEmployee_number();
Intent mListBottomListIntent = new Intent(view.getContext(), MainActivity.class);
mListBottomListIntent.putExtra("Employee_number", mEmployeeNumber);
mListBottomListIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(mListBottomListIntent);

}
});

convertView.setTag(holder);
}
return convertView;
}*/
}

public class CustomCursorWrapper extends CursorWrapper {

private String mStartingEmployeeID = "3500075";
private Employee employee;


public CustomCursorWrapper(Cursor cursor) {
super(cursor);
}

@Override
public int getCount() {

//if you want to exclude just one row
return super.getCount() - 1;
}

@Override
public boolean moveToPosition(int position) {
boolean b = super.moveToPosition(position);
if(employee.getEmployee_number() != null && employee.getEmployee_number().contains(mStartingEmployeeID)) {
//if this is the row you wanna skip
return super.moveToPosition(position + 1);
}

return b;
}
}

最佳答案

通常这是 Tricky,因为您的光标将有该条目。如果条目位于光标内的固定位置,最好是在光标的头部或尾部,这会更容易一些。此外,游标适配器在调用“bindView”时会自动滚动到下一个条目,我不建议直接摆弄游标。如果您仍然想那样做,请记住您不仅必须覆盖 bindview,还必须覆盖适配器上的 getCount 方法。

编辑:或者,如果您要隐藏的项目可以出现在 Cursor 内的动态位置,则使用 Cursor 包装器。基本上 CursorWrapper 环绕 Cursor 并将所有方法调用委托(delegate)给底层游标。但是您可以自由地覆盖某些方法并插入您的业务逻辑。样本是 -

public class CustomCursorWrapper extends CursorWrapper {

public CustomCursorWrapper(Cursor cursor) {
super(cursor);
}

@Override
public int getCount() {

//if you want to exclude just one row
return super.getCount() - 1;
}

@Override
public boolean moveToPosition(int position) {
boolean b = super.moveToPosition(position);
if(false /*check whatever you have to check*/) {
//if this is the row you wanna skip
return super.moveToPosition(position + 1);
}

return b;
}
}

API 文档 - https://developer.android.com/reference/android/database/CursorWrapper.html

请注意上面的示例只是为了展示用法。您可能最终会覆盖更多的方法。试一试

编辑 2 -

Cursor mBottomListCursor = db.rawQuery("SELECT * FROM employees", null);
CursorWrapper myWrapper = new CursorWrapper(mBottomListCursor );

ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
BottomListViewAdapter bottomAdapter = new BottomListViewAdapter(this, myWrapper );
mBottomListView.setAdapter(bottomAdapter);

关于android - 从适配器隐藏数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com