gpt4 book ai didi

java - ListAdapter 是抽象的,无法实例化

转载 作者:行者123 更新时间:2023-11-29 23:51:36 24 4
gpt4 key购买 nike

我想创建一个列表,但我遇到了这些错误我已经尝试了所有我看到的一些帖子说我不应该将 ListAdapter 命名为 ListAdapter 但那也没有用。这是我得到错误的 Activity

public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_list);
final ArrayList<List> detail1 = new ArrayList<>();


detail1.add(new List(R.string.text1, R.string.text11, R.drawable.image1));


ListAdapter adapter = new ListAdapter(this,detail1, R.color.main);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

}}

当我尝试添加一个项目时“列表是抽象的,无法实例化”这是我的列表适配器

public class ListAdapter extends ArrayAdapter<List> {

// Resource ID for the background color for this list of detail
private int mColorResourceId;

// context is the current context (i.e. Activity) that the adapter is being created in
// detail is the list of detail to be displayed.
// colorResourceId is the resource ID for the background color for this list of detail
public ListAdapter(Activity context, ArrayList<List> detail, int colorResourceId) {
super(context, 0, detail);
mColorResourceId = colorResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}

List currentDetails = getItem(position);

TextView detailsTextView = (TextView) listItemView.findViewById(R.id.listName);

detailsTextView.setText(currentDetails.getDetailName());

TextView moreTextView = (TextView) listItemView.findViewById(R.id.listDesc);

moreTextView.setText(currentDetails.getMoreInfo());

// Find the ImageView in the list_item.xml layout with the ID image.

ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);

// Set the ImageView to the image resource specified in the current Details

imageView.setImageResource(currentDetails.getImageResourceId());

// Check if an image is provided for this word or not

if (currentDetails.hasImage()) {

// If an image is available, display the provided image based on the resource ID

imageView.setImageResource(currentDetails.getImageResourceId());

// Make sure the view is visible

imageView.setVisibility(View.VISIBLE);

} else {

// Otherwise hide the ImageView (set visibility to GONE)

imageView.setVisibility(View.GONE);
}

//Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);

//find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);

//set the background color of the text container view
textContainer.setBackgroundColor(color);

// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.

return listItemView;
}

}这是我的 list

public class List {

//Default details
private int mDetailName;

//More Information about the tab
private int mMoreInfo;

// Image resource ID
private int mImageResourceId = NO_IMAGE_PROVIDED;

// Constant value that represents no image was provided for this word
private static final int NO_IMAGE_PROVIDED = 0;

public List(int detailName, int moreInfo, int imageResourceId) {
mDetailName = detailName;
mMoreInfo = moreInfo;
mImageResourceId = imageResourceId;
}
// Get the details
public int getDetailName() {
return mDetailName;
}
// Get more info of the tab
public int getMoreInfo() {
return mMoreInfo;
}
// Create a new object
public int getImageResourceId() { return mImageResourceId; }

// Returns whether or not there is an image for this word.
public boolean hasImage() { return mImageResourceId != NO_IMAGE_PROVIDED; }

}我不明白我做错了什么

最佳答案

final ArrayList<List>

List是内置接口(interface) https://docs.oracle.com/javase/6/docs/api/java/util/List.html

您的代码中是否有“java.util.List”的导入?

关于java - ListAdapter 是抽象的,无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50843723/

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