簡體   English   中英

具有TextView的RecyclerView適配器

[英]RecyclerView Adapter with TextView

我的RecyclerView單行有3列。 2是從SQLite數據庫查詢中填充的,另一個是由TextView填充的。 我在如何填充TextView遇到麻煩。 目的是通過從一組switch語句中選擇的Strings選項來設置TextView 但是,無法解析findViewById 有沒有辦法在onBindViewHolder有一個TextView

RecyclerViewAdapter.java

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

private Cursor mCursor;


public UpcomingRecyclerViewAdapter(Cursor cursor) {
    this.mCursor = cursor;
}

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

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_items, parent, false);
    return new UpcomingRecyclerViewAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(UpcomingRecyclerViewAdapter.ViewHolder holder, int position) {
    if ((mCursor != null) && (mCursor.getCount() != 0)) {
        if (!mCursor.moveToPosition(position)) {
            throw new IllegalStateException("Couldn't move cursor to position " + position);
        }
        String name = mCursor.getString(mCursor.getColumnIndex(VehiclesContract.Columns.VEHICLE_NAME));
        String miles = mCursor.getString(mCursor.getColumnIndex(VehiclesContract.Columns.VEHICLE_CURRENT));
        String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);

        holder.name.setText(name);
        holder.miles.setText(miles);
        holder.maintenance.setText(maintenance);
    }
}


@Override
public int getItemCount() {
    return mCursor != null ? mCursor.getCount() : 0;
}


static class ViewHolder extends RecyclerView.ViewHolder {
    TextView name;
    TextView miles;
    TextView maintenance;

    public ViewHolder(View itemView) {
        super(itemView);
        this.name = itemView.findViewById(R.id.upcoming_name);
        this.miles = itemView.findViewById(R.id.upcoming_miles);
        this.maintenance = itemView.findViewById(R.id.upcoming_maintenance);
    }
}

好吧,在這條線

String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);

您正在嘗試 視圖 分配給 string ,這是完全錯誤的。

您可以assign the value of a textfield which is a string to a string variable

assign a textfield to a variable of textfield type.

首先解決此問題,然后查看它是否可以解決您的問題。

String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);

findViewById返回一個View。 您正在嘗試將其轉換為字符串。 嘗試這個:

String maintenance = ((TextView) view.findViewById(R.id.up_list_maintenance)).getText().toString();

暫無
暫無

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

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