gpt4 book ai didi

java - 错误: cannot find symbol variable itemsAdapter

转载 作者:行者123 更新时间:2023-12-01 18:40:31 27 4
gpt4 key购买 nike

我正在遵循教程,但无法成功运行我的项目。我不断收到错误:找不到符号变量 itemsAdapter,即使我的类位于同一文件夹中。有任何想法吗?谢谢 MainActivity.java itemsAdapter.javaMainActivity.java:

package com.example.simpletodo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;


import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity {

List<String> items;

Button btnAdd;
EditText etItem;
RecyclerView rvItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnAdd = findViewById(R.id.btnAdd);
etItem = findViewById(R.id.etItem);
rvItems = findViewById(R.id.rvItems);

items = new ArrayList<>();
items.add("Buy milk");
items.add("Go to the gym");
items.add("Email Autumn");

itemsAdapter ItemsAdapter;
ItemsAdapter = new itemsAdapter(items);
rvItems.setAdapter(itemsAdapter);
rvItems.setLayoutManager(new LinearLayoutManager(this));

}
}

ItemsAdapter.java: 包 com.example.simpletodo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

List<String> items;

public itemsAdapter(List<String> items) {
this.items = items;

}

// @NonNull
@Override
// creates each view
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Use layout inflator to inflate a view
View todoView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
// wrap it inside a View Holder and return it
return new ViewHolder(todoView);
}

// responsible for binding data to a particular view holder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// Grab the item at the position
String item = items.get(position);
// Bind the item into specified view holder
holder.bind(item);

}

// the # of items available in the data
@Override
public int getItemCount() {
return 0;
}

// Container to provide easy access to views that represent each row of the list
class ViewHolder extends RecyclerView.ViewHolder {

public ViewHolder(@NonNull View itemView) {
super(itemView);
}

// Update the view inside of the view holder with this data
public void bind(String item) {
}
}

最佳答案

MainActivity.java的第41行中,itemsAdapter需要是ItemsAdapter

PS:您的 itemsAdapter 命名标准与惯例相反。该类应命名为 ItemsAdapter,变量/实例应为 itemsAdapter

关于java - 错误: cannot find symbol variable itemsAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59940517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com