我正在使用RecyclerView和Adapter,我使用setClickListener()来单击View上的项目,它将转换到另一个活动。但我面临关于ItemClickListener接口类的问题。
我的界面课程:
public interface ItemClickListener {
void onClick(View view, int position);
}
我的适配器:
public class RecyclerViewCategoryAdapter extends RecyclerView.Adapter<RecyclerViewCategoryAdapter.ViewHolder> {
Context context;
ImageLoader imageThumbLoader;
private ItemClickListener clickListener;
public RecyclerViewCategoryAdapter(ArrayList<HashMap<String, String>> getCarData, Context context){
super();
this.getCarData = getCarData;
this.context = context;
}
@Override
public RecyclerViewCategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_car_info, parent, false);
RecyclerViewCategoryAdapter.ViewHolder viewHolder = new RecyclerViewCategoryAdapter.ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewCategoryAdapter.ViewHolder viewholder, int position) {
...
}
public int getDataAdapter() {
return getCarData == null ? 0 : getCarData.size();
}
@Override
public int getItemCount() {
return getDataAdapter(); // + 1;
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView titleName;
public NetworkImageView imageThumb;
public ViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.titleName);
imageThumb = (NetworkImageView) itemView.findViewById(R.id.imageThumb);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
}
}
}
我的RecyclerView类(CategoryCarActivity)更新:
public class CategoryCarActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener, ItemClickListener {
RecyclerViewCategoryAdapter recyclerViewAdapter;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewLayoutManager;
private LinearLayout linearLayoutScroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_category_adapter);
...
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerViewLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
new LoadCategoryCars().execute();
recyclerViewAdapter.setClickListener(this);
}
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);
String car_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
String price = ((TextView) view.findViewById(R.id.price)).getText().toString();
Toast.makeText(getApplicationContext(), "Car Id: " + car_id + ", Price: " + price, Toast.LENGTH_SHORT).show();
i.putExtra("car_id", car_id);
i.putExtra("price", price);
Log.i("Car ID: ", car_id);
Log.i("Price: ", price);
startActivity(i);
}
// Initialize Toolbar the first time
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_icon_toolbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
/**
* Background Async Task to Load all tracks under one album
* */
class LoadCategoryCars extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CategoryCarActivity.this);
pDialog.setMessage("Loading cars ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting tracks json and parsing
* */
protected String doInBackground(String... args) {
...
} catch (JSONException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
super.onPostExecute(result);
recyclerViewAdapter = new RecyclerViewCategoryAdapter(carsList, CategoryCarActivity.this);
recyclerViewAdapter.notifyDataSetChanged();
recyclerView.setAdapter(recyclerViewAdapter);
// dismiss the dialog after getting all manufacturers
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
在行(更新):
public class CategoryCarActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener, ItemClickListener {
编译器发生错误消息(更新):
Class 'CategoryCarActivity' must either be declared abstract or implement abstract method 'onClick(View, int)' in 'ItemClickListener'
如何解决问题?非常感谢!
答案 0 :(得分:1)
您的活动需要实现界面!
public class CategoryCarActivity extends AppCompatActivity implements
AbsListView.OnScrollListener, AbsListView.OnItemClickListener,
ItemClickListener {
public void onClick(View view, int position) {
// do your stuff here.
}
===============
要修复最新的问题,您必须覆盖活动中界面中声明的抽象方法。为此,请替换:
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);
String car_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
String price = ((TextView) view.findViewById(R.id.price)).getText().toString();
Toast.makeText(getApplicationContext(), "Car Id: " + car_id + ", Price: " + price, Toast.LENGTH_SHORT).show();
i.putExtra("car_id", car_id);
i.putExtra("price", price);
Log.i("Car ID: ", car_id);
Log.i("Price: ", price);
startActivity(i);
}
使用:
@Override
public void onClick(View view, int position) {
Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);
String car_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
String price = ((TextView) view.findViewById(R.id.price)).getText().toString();
Toast.makeText(getApplicationContext(), "Car Id: " + car_id + ", Price: " + price, Toast.LENGTH_SHORT).show();
i.putExtra("car_id", car_id);
i.putExtra("price", price);
Log.i("Car ID: ", car_id);
Log.i("Price: ", price);
startActivity(i);
}