我需要不同的列表到一个listView(取决于使用它的类)。像这样:
private static List<Product> catalogA;
private static List<Product> catalogB;
private static List<Product> catalogC;
public static List<Product> getCatalogA(Resources res){
if(catalogA == null) {
catalogA = new Vector<Product>();
catalogA.add(new Product("product1", 1));
catalogA.add(new Product("product2", 2));
}
return catalogA;
}
public static List<Product> getCatalogB(Resources res){
if(catalogB == null) {
catalogB = new Vector<Product>();
catalogB.add(new Product("product3", 3));
catalogB.add(new Product("product4", 4));
}
return catalogB;
}
另外,我需要包含catalogA和catalogB的资源。它看起来像这样:
public static List<Product> getCatalogC(Resources res){
if(catalogC == null) {
catalogC = new Vector<Product>();
catalogC.addAll(catalogA);
catalogC.addAll(catalogB);
}
return catalogC;
}
然后在我的两个不同的类中,我正在尝试获取此资源,将Adapter设置为ListView,并设置OnItemClickListener。像这样: 头等舱:
mProductList = ResHelper.getCatalogA(getResources());
ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
mProductAdapter = new ProductAdapter(mProductList, getLayoutInflater());
listViewCatalog.setAdapter(mProductAdapter);
listViewCatalog.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent productDetailsIntent = new Intent(getBaseContext(),DetailsActivity.class);
DetailsIntent.putExtra(ResHelper.PRODUCT_INDEX, position);
startActivity(DetailsIntent);
}
});
第二课(几乎相同):
mProductList = ResHelper.getCatalogB(getResources());
ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
mProductAdapter = new ProductAdapter(mProductList, getLayoutInflater());
listViewCatalog.setAdapter(mProductAdapter);
listViewCatalog.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent productDetailsIntent = new Intent(getBaseContext(),DetailsActivity.class);
DetailsIntent.putExtra(ResHelper.PRODUCT_INDEX, position);
startActivity(DetailsIntent);
}
});
这是我在课堂上必须在点击项目时开始的内容:
List<Product> catalogC = ResHelper.getCatalogC(getResources());
int productIndex = getIntent().getExtras().getInt(ResHelper.PRODUCT_INDEX);
final Product selectedProduct = catalogC.get(productIndex);
我的问题是:除OnItemClick外,一切正常。它不起作用。我不确定是因为“位置”还是因为类中的catalogC必须启动OnItemClick。但我不需要另一个适配器或其他ListView。 OnItemClick在两个类中也必须具有相同的功能。有可能吗?