2017-07-08 1 views
-2

J'utilise cardview pour afficher les éléments dans gridview. Je veux ouvrir une nouvelle activité mais selon mon code, il montre le toast à propos de la position de l'objet. Je n'arrive pas où je me trompe alors s'il vous plaît aidez-moi. L'aide serait appréciée, merci d'avance.ne peut pas ouvrir une nouvelle activité lorsque vous cliquez sur cardview mais il montre toast seulement

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

} 

classe 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(); 

     } 
    } 

après modification je reçois une erreur dans la commande intent.putExtra

@Override onItemClick public void (point DataModel) {

Intent intent = new Intent(MainActivity.this, Yojana.class); 
    intent.putExtra("dataitem", item); 
    startActivity(intent); 

} 

Yojana.class

public class étend 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) 

Répondre

0

cela ouvre une nouvelle activité .mais comment ouvrir différentes activités en cliquant sur différentes vues de cartes?

@Override public void onItemClick (point DataModel) {

 Intent intent = new Intent(MainActivity.this, Yojana.class); 
     intent.putExtra("dataitem", String.valueOf(item)); 
     startActivity(intent); 


} 

Yojana.class

public class Yojana étend AppCompatActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_yojana); 
    getIntent().getExtras().getString("dataitem"); 

} 
+0

@ deadpool007 maintenant comment ouvrir différentes activités. comment mettre des conditions à ce sujet afin que je puisse ouvrir une activité différente –

0

Il montre du pain grillé parce que vous avez écrit ci-dessous le code:

Toast.makeText(getApplicationContext(), item.text + " is clicked", Toast.LENGTH_SHORT).show(); 

Insted vous devriez écrire ci-dessous le code:

Intent intent = new Intent(MainActivity.this, NextActivity.class); 
intent.putExtra("dataitem", item); 
startActivity(intent); 
+0

. getIntent(). getExtras(). getString ("dataitem"); J'ai écrit ceci dans une autre activité mais je reçois toujours l'erreur . Erreur: (56, 15) erreur: aucune méthode appropriée trouvée pour putExtra (String, DataModel) méthode Intent.putExtra (String, boolean) n'est pas applicable (discordance d'argument, DataModel ne peut pas être converti en booléen) méthode Intent.putExtra (String, byte) n'est pas applicable (argument discordance, DataModel ne peut pas être converti en octet) –

+0

pas de rendre votre classe Dataitem PARCELABLE qui est implémentée Interface parcelable pour votre classe comme vous envoyez un objet entier à une autre activité. et dans NextActivity, faites ceci getIntent().getParcelableExtra ("dataitem") – Shrikant

+0

toujours obtenir la même erreur que ci-dessus –