【问题标题】:Android : Updating row layouts based on User Input with a CursorAdapterAndroid:使用 CursorAdapter 根据用户输入更新行布局
【发布时间】:2017-05-28 21:14:45
【问题描述】:

我目前正在开发一个应用程序,它在列表视图中显示来自 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 添加额外数据?

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

【问题讨论】:

  • 鉴于您与我们分享的内容,我建议您创建一个“view_entry”部分不可见的单一布局。只有当 bindView 中的 "cursor.getPosition() == viewRequestPosition" 时,才使这些部分可见,反之亦然。

标签: android listview layout android-cursoradapter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    相关资源
    最近更新 更多