我正在尝试使用自定义ListView适配器切换到不同的活动,但我遇到了麻烦,因为有一个我似乎无法摆脱的错误。
相关代码 分类表:
public class CategoryTable extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_table);
populateTable();
goToPreviousActivity();
}
private void populateTable() {
final ListView mListView = findViewById(R.id.listView);
Category 1 = new Category(" One");
Category 2 = new Category(" Two");
Category 3 = new Category(" Three");
Category 4 = new Category(" Four");
final ArrayList<Category> categoryList = new ArrayList<>();
categoryList.add(1);
categoryList.add(2);
categoryList.add(3);
categoryList.add(4);
CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
mListView.setAdapter(adapter);
}
private void goToPreviousActivity() {
ImageButton btn = findViewById(R.id.backButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public static Intent makeIntent(Context context) {
return new Intent(context, CategoryTable.class);
}
}
类别列表适配器:
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
final LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
catName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == 0) {
//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
mContext.startActivity(intent);
} else {
Toast toast = Toast.makeText(getContext(), "Clicked2", Toast.LENGTH_SHORT);
toast.show();
}
}
});
return convertView;
}
}
(我在错误的代码段上面评论过)
所有工具表:
public class AllToolsTable extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_tools_table);
}
public static Intent makeIntent(Context context) {
return new Intent(context, AllToolsTable.class);
}
}
如果需要,我会提供任何其他相关的代码/细节。
非常感谢任何帮助。
答案 0 :(得分:1)
propertyToUnwind
您的includeArrayIndex
方法需要//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
个参数。通常情况下,您可以使用AllToolsTable.makeIntent()
之类的内容,因为Context
是AllToolsTable.makeIntent(MainActivity.this)
的子类。但是,Activity
不是。
所以你需要以某种方式接触Context
对象。
幸运的是,CategoryListAdapter
方法会传递一个非null Context
对象,并且所有视图都有一个上下文。因此,您可以使用以下代码替换您的意图创建:
getView()