2015-09-25 40 views
0

我想從以下各項目開始新的活動我怎樣才能做到這一點 這裏是我的卡適配器活動從Cardlist項目陣列開始新的活動

package com.masiliose.denta; 

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 




public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> { 

    List<NatureItem> mItems; 

    public CardAdapter() { 
     super(); 
     mItems = new ArrayList<NatureItem>(); 
     NatureItem nature = new NatureItem(); 
     nature.setName("HIV/AIDS"); 
     nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" + 
         "ut labore et dolore magna aliqua. Ut enim ad minim veniam."); 
     nature.setThumbnail(R.drawable.ccc); 
     mItems.add(nature); 
+0

你試過谷歌嗎?這是一個簡單的自定義適配器。有幾千個例子... BTW你的代碼不滿。 –

+0

什麼是「NatureItem」?這是一個自定義視圖嗎?如果是這樣的話,這可能只是一個簡單的情況,即爲其設置一個OnClick監聽器,併發出一個打開其中所需活動的意圖。我覺得這個問題是缺少的東西... – aravindsagar

+0

是的,這是一個自定義的視圖。我只想要的是能夠從列表中開始新的活動 –

回答

0

希望的片斷這將是有幫助您。

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{ 

private List<NatureItem> mItems; 
private Context mContext; 

/* 
pass a context via consectetur, it will be necessary to start activity on click card 
*/ 
public CardAdapter(Context mContext){ 
    this.mContext = mContext; 

    mItems = new ArrayList<NatureItem>(); 
    NatureItem nature = new NatureItem(); 
    nature.setName("HIV/AIDS"); 
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" + 
        "ut labore et dolore magna aliqua. Ut enim ad minim veniam."); 
    nature.setThumbnail(R.drawable.ccc); 
    mItems.add(nature); 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    /* 
    your custom card item layout 
    */ 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false); 
    ViewHolder viewHolder = new ViewHolder(v); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, final int i) { 

    final String name = mItems.get(i).getName(); 

    viewHolder.nameView.setName(name); 
    /*....... 
    set values to other view's 
    .......*/ 

    viewHolder.cv.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(mContext, YourActivity.class); 
      mContext.startActivity(intent); 
     } 
    }); 


} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

@Override 
public int getItemCount() { 
    return mItems.size(); 
} 

public static class ViewHolder extends RecyclerView.ViewHolder { 
    CardView cv; 
    TextView nameView; 

    ViewHolder(View itemView) { 
     super(itemView); 
     cv = (CardView)itemView.findViewById(R.id.cardView); 
     nameView = (TextView)itemView.findViewById(R.id.nameView); 

    } 
} } 
+0

請在代碼中添加一些解釋。這對OP來說可能是有用的。 – user2339071

+0

嘿數組列表包含雖然我只有一個在該代碼片段許多項目不只一個。所以這些項目不僅僅是開放不同的活動 –