2017-07-11 83 views
0

我有一個recyclerview適配器,它在列表視圖中顯示通過光標填充數據的數據。我想進一步擴展,通過設置點擊每個列表項目,並將值的意圖傳遞給另一個活動,如細節活動。將遊標對象通過意圖傳遞給Android中的其他活動

Adapter.class

public class RecyclerAdapter extends 
    RecyclerView.Adapter<RecyclerAdapter.Holder> { 

/* ViewHolder for each insect item */ 
public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    TextView friendlyName, scientificName, dangerLevel; 
    ImageView image; 


    public Holder(View itemView) { 
     super(itemView); 


     friendlyName = (TextView) itemView.findViewById(R.id.friendlyName); 
     scientificName = (TextView) itemView.findViewById(R.id.scientificName); 
     dangerLevel = (TextView) itemView.findViewById(R.id.text1); 



    } 

    @Override 
    public void onClick(View v) { 

    } 
} 

private Cursor mCursor; 
private Context mContext; 

public RecyclerAdapter(Context context, Cursor cursor) { 
    this.mContext = context; 
    this.mCursor = cursor; 
} 

@Override 
public Holder onCreateViewHolder(ViewGroup parent, int viewType) { 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    View view = inflater.inflate(R.layout.bugs_list_item, parent, false); 
    return new Holder(view); 
} 


@Override 
public void onBindViewHolder(Holder holder, int position) { 

    int insectname = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME); 
    int scienceName = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME); 
    int id = mCursor.getColumnIndex(BugsContract.BugsEntry._ID); 
    int dangerlevel = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL); 
    int insectImage = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET); 

    mCursor.moveToPosition(position); 

    String insectRName = mCursor.getString(insectname); 
    String scienceRName = mCursor.getString(scienceName); 
    String insectRImage = mCursor.getString(insectImage); 
    int dangerlevelInt = mCursor.getInt(dangerlevel); 


    String dangerString = "" + dangerlevelInt; 

    holder.dangerLevel.setText(dangerString); 

    holder.friendlyName.setText(insectRName); 
    holder.scientificName.setText(scienceRName); 


} 

private int getDangerColor(int danger) { 
    int priorityColor = 0; 
    int[] colorDangerarray = mContext.getResources().getIntArray(R.array.dangerColors); 

    switch(danger) { 
     case 1: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[0]); 
      break; 
     case 2: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[1]); 
      break; 
     case 3: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[2]); 
      break; 
     default: break; 
    } 
    return priorityColor; 
} 

@Override 
public int getItemCount() { 
    return mCursor.getCount(); 
} 

/** 
* Return the {@link Insect} represented by this item in the adapter. 
* 
* @param position Adapter item position. 
* 
* @return A new {@link Insect} filled with this position's attributes 
* 
* @throws IllegalArgumentException if position is out of the adapter's bounds. 
*/ 
public Insect getItem(int position) { 
    if (position < 0 || position >= getItemCount()) { 
     throw new IllegalArgumentException("Item position is out of adapter's range"); 
    } else if (mCursor.moveToPosition(position)) { 
     return new Insect(mCursor); 
    } 
    return null; 
} 
} 

模型類,它實現parceleable

public class Insect implements Parcelable { 
private static final String TAG = Insect.class.getSimpleName(); 


//Common name 
public int id; 

public String name; 
//Latin scientific name 
public String scientificName; 
//Classification order 
public String classification; 
//Path to image resource 
public String imageAsset; 
//1-10 scale danger to humans 
public int dangerLevel; 

/** 
* Create a new Insect from discrete values 
*/ 
public Insect(String name, String scientificName, String classification, String imageAsset, int dangerLevel) { 
    this.name = name; 
    this.scientificName = scientificName; 
    this.classification = classification; 
    this.imageAsset = imageAsset; 
    this.dangerLevel = dangerLevel; 
} 

/** 
* Create a new Insect from a database Cursor 
*/ 
public Insect(Cursor cursor) { 
    //TODO: Create a new insect from cursor 

    this.name = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME)); 
    this.scientificName = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)); 
    this.classification = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_CLASSIFICATION)); 
    this.imageAsset = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET)); 
    this.dangerLevel = Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL))); 
} 

public Insect() { 

} 

/** 
* Create a new Insect from a data Parcel 
*/ 
protected Insect(Parcel in) { 
    this.name = in.readString(); 
    this.scientificName = in.readString(); 
    this.classification = in.readString(); 
    this.imageAsset = in.readString(); 
    this.dangerLevel = in.readInt(); 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(name); 
    dest.writeString(scientificName); 
    dest.writeString(classification); 
    dest.writeString(imageAsset); 
    dest.writeInt(dangerLevel); 
} 


@Override 
public int describeContents() { 
    return 0; 
} 

public static final Creator<Insect> CREATOR = new Creator<Insect>() { 
    @Override 
    public Insect createFromParcel(Parcel in) { 
     return new Insect(in); 
    } 

    @Override 
    public Insect[] newArray(int size) { 
     return new Insect[size]; 
    } 
}; 
} 

我想知道如何聲明,該數據容納到另一個活動的意圖和如何從獲取數據在下一個活動的意圖

回答

0

只要你已經在你的模型類上實現了parcelable,你可以很容易地傳遞你的對象,就像你通常會p通過putExtra()方法來確定原始數據。

對於發件人活動:

Intent receiverIntent = new Intent(this, Receiver.class); 
receiverIntent.putExtra("key", ParcelableObject); 
startActivity(receiverIntent); 

至於接收機類,您只需打一個意圖對象來獲取parcelableExtraonCreate()方法。

Intent i = getIntent(); 
ParcelableClass parcelableObject; 
parcelableObject = i.getParcelableExtra("key"); 

瞧,應該這樣做。

+0

現在ParcelableObject應該是遊標或模型對象,這就是你不清楚的答案什麼是ParcelableObject – Delaroy

+0

顯然它應該來自你的'Insect'類對象,因爲你將'cursor'傳遞給了類的構造函數(意味着昆蟲對象是從你的光標構造的),傳遞'Insect'對象應該滿足你的需求。 :) – Andy

相關問題