我正在使用cardview
来显示gridview中的项目。我想打开一个新的活动,但根据我的代码,它显示了关于项目位置的祝酒词。我没有得到我犯错的地方所以请帮助我。感谢提前帮助。
DataModel.class
public class DataModel {
public String text;
public int drawable;
public String color;
public DataModel(String t, int d, String c )
{
text=t;
drawable=d;
color=c;
}
}
RecyclerViewAdapter.class
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<DataModel> mValues;
Context mContext;
protected ItemListener mListener;
public RecyclerViewAdapter(Context context, ArrayList<DataModel> values, ItemListener itemListener) {
mValues = values;
mContext = context;
mListener=itemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textView;
public ImageView imageView;
public RelativeLayout relativeLayout;
DataModel item;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(this);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
}
public void setData(DataModel item) {
this.item = item;
textView.setText(item.text);
imageView.setImageResource(item.drawable);
relativeLayout.setBackgroundColor(Color.parseColor(item.color));
}
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onItemClick(item);
}
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder Vholder, int position) {
Vholder.setData(mValues.get(position));
}
@Override
public int getItemCount() {
return mValues.size();
}
public interface ItemListener {
void onItemClick(DataModel item);
}
}
MainActivity类
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.ItemListener {
RecyclerView recyclerView;
ArrayList<DataModel> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
arrayList = new ArrayList<>();
arrayList.add(new DataModel("Item 1", R.drawable.downtoearth, "#FFFFFF"));
arrayList.add(new DataModel("Item 2", R.drawable.beer, "#3E51B1"));
arrayList.add(new DataModel("Item 3", R.drawable.ferrari, "#673BB7"));
arrayList.add(new DataModel("Item 4", R.drawable.jetpack_joyride, "#4BAA50"));
arrayList.add(new DataModel("Item 5", R.drawable.three_d, "#F94336"));
arrayList.add(new DataModel("Item 6", R.drawable.terraria, "#0A9B88"));
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, arrayList, this);
recyclerView.setAdapter(adapter);
/**
AutoFitGridLayoutManager that auto fits the cells by the column width defined.
**/
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(this, 300);
recyclerView.setLayoutManager(layoutManager);
/**
Simple GridLayoutManager that spans two columns
**/
GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
}
@Override
public void onItemClick(DataModel item) {
//here toast is
Toast.makeText(getApplicationContext(), item.text + " is clicked",
Toast.LENGTH_SHORT).show();
}
}
编辑后我在intent.putExtra命令中收到错误
@覆盖 public void onItemClick(DataModel item){
Intent intent = new Intent(MainActivity.this, Yojana.class);
intent.putExtra("dataitem", item);
startActivity(intent);
}
Yojana.class
公共课Yojana扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yojana);
getIntent().getParcelableExtra("dataitem");
}
logcaterror:
Error:(56, 15) error: no suitable method found for
putExtra(String,DataModel)
method Intent.putExtra(String,boolean) is not applicable
(argument mismatch; DataModel cannot be converted to boolean)
method Intent.putExtra(String,byte) is not applicable
(argument mismatch; DataModel cannot be converted to byte)
method Intent.putExtra(String,char) is not applicable
(argument mismatch; DataModel cannot be converted to char)
method Intent.putExtra(String,short) is not applicable
(argument mismatch; DataModel cannot be converted to short)
method Intent.putExtra(String,int) is not applicable
(argument mismatch; DataModel cannot be converted to int)
method Intent.putExtra(String,long) is not applicable
(argument mismatch; DataModel cannot be converted to long)
method Intent.putExtra(String,float) is not applicable
(argument mismatch; DataModel cannot be converted to float)
method Intent.putExtra(String,double) is not applicable
(argument mismatch; DataModel cannot be converted to double)
method Intent.putExtra(String,String) is not applicable
(argument mismatch; DataModel cannot be converted to String)
method Intent.putExtra(String,CharSequence) is not applicable
(argument mismatch; DataModel cannot be converted to CharSequence)
method Intent.putExtra(String,Parcelable) is not applicable
(argument mismatch; DataModel cannot be converted to Parcelable)
method Intent.putExtra(String,Parcelable[]) is not applicable
(argument mismatch; DataModel cannot be converted to Parcelable[])
method Intent.putExtra(String,Serializable) is not applicable
(argument mismatch; DataModel cannot be converted to Serializable)
method Intent.putExtra(String,boolean[]) is not applicable
(argument mismatch; DataModel cannot be converted to boolean[])
method Intent.putExtra(String,double[]) is not applicable
(argument mismatch; DataModel cannot be converted to double[])
method Intent.putExtra(String,String[]) is not applicable
(argument mismatch; DataModel cannot be converted to String[])
method Intent.putExtra(String,CharSequence[]) is not applicable
(argument mismatch; DataModel cannot be converted to CharSequence[])
method Intent.putExtra(String,Bundle) is not applicable
(argument mismatch; DataModel cannot be converted to Bundle)
答案 0 :(得分:0)
它显示吐司,因为你写了下面的代码:
export MONGO_URL=mongodb://localhost:3001/meteor meteor run
你应该写下以下代码:
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\nodejs\\node.exe"
"C:\\Users\\Nico\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "meteor"
npm ERR! node v6.9.2
npm ERR! npm v4.0.2
npm ERR! code ELIFECYCLE
npm ERR! crypto@ meteor: `export MONGO_URL=mongodb://localhost:3001/meteor meteor run`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the crypto@ meteor script 'export MONGO_URL=mongodb://localhost:3001/meteor meteor'.
答案 1 :(得分:0)
这会打开一个新活动。但是如何通过点击不同的卡片视图打开不同的活动?
@覆盖 public void onItemClick(DataModel item){
Intent intent = new Intent(MainActivity.this, Yojana.class);
intent.putExtra("dataitem", String.valueOf(item));
startActivity(intent);
}
Yojana.class
公共课Yojana扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yojana);
getIntent().getExtras().getString("dataitem");
}