Android:使用 CursorAdapter 根据用户输入更新行布局

问题描述 投票:0回答:1

我目前正在开发一个应用程序,该应用程序在列表视图中显示来自 SQLiteDatabase 的数据。该列表视图使用自定义布局。我的目的是根据用户在做什么来拥有三种不同的布局。第一个布局仅显示一个 TextView 和一个 Button(称为 row_nameonly)。如果用户按下此按钮,布局将切换到更详细的视图(称为 row_viewentry)。最后,如果用户再次按下该按钮,则将再次显示第一个布局(row_nameonly)。我曾尝试完成此任务,但尚未找到可行的解决方案。我当前的版本似乎更改了行的视图,但新的布局不可见。我希望这样做而不必向数据库添加额外的数据。 这是自定义 CursorAdapter 的代码:

public class EntryListCursorAdapter extends CursorAdapter{

public int viewRequestPosition = -100;

public EntryListCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.row_nameonly, parent, false);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {

    ViewGroup parent = (ViewGroup) view.getParent();

    if(cursor.getPosition() == viewRequestPosition){
        view = LayoutInflater.from(context).inflate(R.layout.row_viewentry, parent, false);
    }

    if(!(cursor.getPosition() == viewRequestPosition)) {
        TextView nameView = (TextView) view.findViewById(R.id.txt_name);
        ImageButton expandMoreButton = (ImageButton) view.findViewById(R.id.btn_expandmore);

        String name = cursor.getString(cursor.getColumnIndexOrThrow("prename")) + " " +
                cursor.getString(cursor.getColumnIndexOrThrow("surname"));

        nameView.setText(name);

        expandMoreButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                viewRequestPosition = cursor.getPosition();
                notifyDataSetChanged();
            }
        });
    }
}

TLDR:有没有办法让 CursorAdapter 更改单击按钮的视图的布局,并显示新的布局,而不向 SQLiteDatabase 添加额外的数据?

谢谢你 (如果您需要更多信息,请询问)

android listview layout android-cursoradapter
1个回答
0
投票

如果有人好奇, 我让适配器获取在其构造函数中调用它的片段。然后我保存了我想要更改布局的 ListItems 的位置,该片段称为适配器。然后我创建了一个新的适配器实例,但这次布局将根据我在片段中保存的位置进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.