我正在学习教程,但无法使我的项目成功运行。我不断收到错误消息:即使我的课程在同一个文件夹中,也找不到符号变量itemsAdapter。有任何想法吗?谢谢
MainActivity.java
itemsAdapter.java
MainActivity.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