簡體   English   中英

setText() 在適配器內不起作用

[英]setText() not working inside the adapter

我正在做一個 Adroid 應用程序來練習一點,但我在這里發現了一些麻煩。 我有這個適配器來列出一些產品。 我可以在活動中看到它們,但 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();
}

文本視圖 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 設置為白色。 天才。

謝謝!

適配器基於計數進行迭代,即cursor.getCount() 您可以使用它的位置從索引中獲取項目。 但是在您的情況下,您必須迭代自己的。 否則每次你的完整塊被調用時都不會有結果。

嘗試:-

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

最后調用notifyItemPositionChanged()並傳遞相同的位置以使視圖刷新。

您需要檢查 full 是否為 true 且 name 不為 null。如果 name 為 null 或為空,那么它將不會設置任何其他內容,即在 Textview 上的 name 變量中設置任何內容。因此,請調試或添加 Log 以打印 name 中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM