如何使用自定义 ListView 适配器切换活动?

How to Switch Activities using a Custom ListView Adapter?

我正在尝试使用自定义 ListView 适配器来切换到不同的活动,但在这样做时遇到了问题,因为存在一个我似乎无法摆脱的错误。

相关代码 类别 Table:

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;
}

}

(我在错误的代码上面注释了)

所有工具Table:

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);
}
}

如果需要,我会提供任何其他相关的 code/details。

任何帮助将不胜感激。

//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) 这样的东西,因为 ActivityContext 的子类。但是,CategoryListAdapter 不是。

所以你需要以某种方式得到一个 Context 对象。

幸运的是,getView() 方法向您传递了一个非空 ViewGroup parent 对象,并且所有视图都有一个上下文。所以你可以用这段代码替换你的意图创建:

Context context = parent.getContext();
Intent intent = AllToolsTable.makeIntent(context);