setText() 在适配器内部不工作

setText() not working inside the adapter

我正在做一个 Android 应用程序来练习一下,但我在这里发现了一些麻烦。我有这个适配器来列出一些产品。我可以在 activity 中看到它们,但 TextView 是空的。

您知道为什么 setText() 函数不起作用吗? 我认为这将是一个愚蠢的语法错误或类似的错误,但我找不到它。

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

private Cursor cursor;
private boolean full;

public CardAdapter(Cursor c) {
    cursor = c;
    full = cursor.moveToFirst();
}

@Override
public CardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.product_row, parent, false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    try {
        if (full) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                holder.productName.setText(name);
                full = cursor.moveToNext();
                if(!full) cursor.close();
        }
    } catch (Exception e) {
        Log.d("ADAPTER", "Error while trying to get rows from database");
    }
}

@Override
public int getItemCount() {
    int i = cursor.getCount();
    if(i > 0) {
        full = true;
        return i;
    }
    else {
        full = false;
        return 0;
    }
}

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView productName;
    public ViewHolder(View v) {
        super(v);
        productName = ((TextView) v.findViewById(R.id.productName));
    }
 }
}

更新: 更改 onBindViewHolder:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    try {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));

                holder.productName.setText(name);
                notifyItemChanged(position);
            } while (cursor.moveToNext());

        }
    } catch (Exception e) {
        Log.d("ADAPTER", "Error while trying to get rows from database");
    }
    cursor.close();
}

TextView xml:

<TextView
                android:id="@+id/productName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:textStyle="bold"
                />

在 mainACtivity 中,onCreate() 中有这个:

    pRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    pRecyclerView.setHasFixedSize(true);
    pLayoutManager = new LinearLayoutManager(this);

    productManager = new ProductManager(this);
    Cursor c = productManager.getProducts();

    pAdapter = new CardAdapter(c);
    pRecyclerView.setAdapter(pAdapter);
    pRecyclerView.setLayoutManager(pLayoutManager);

解决方案 因为它也在我在这里发布的评论中:textColor 被设置为白色。天才.

谢谢!

您需要检查 full 是否为 true 且 name 不是 null.If name 为 null 或空然后它将不设置任何其他设置 Textview.So 上的 name 变量中的任何内容请调试或添加登录以打印名称中的值。

适配器根据计数进行迭代,即 cursor.getCount()。您可以使用它的位置从索引中获取项目。但在你的情况下,你必须自己迭代。否则每次你的完整块被调用时都会没有结果。

尝试:-

if(cursor.moveToFirst()) {
   do {
       String name = cursor.getString(cursor.getColumnIndex("name"));
   }while (cursor.moveToNext());
}
cursor.close();

最后调用 notifyItemPositionChanged() 并传递相同的位置以刷新视图。