0

我有一個通過與Android數據綁定技術recyclerview填充項目的列表,現在我想傳遞和填充相同的數據詳細活動沒有得到正確的事情做這樣。所以,請幫助做到這一點。通過Android數據綁定從回收站視圖onClick到細節活動的數據

公共類片段延伸片段{

private FirebaseRecyclerAdapter adapter; 
private Firebase mFirebaseRef = new Firebase("https://xyz.firebaseio.com/category/").child("list"); 

public Fragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    final View rootView = inflater.inflate(R.layout.recycler_view, container, false); 
    final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); 

    adapter = new FirebaseRecyclerAdapter<List, ViewHolder>List.class, R.layout.fragment, 
      ViewHolder.class, mFirebaseRef) { 
     @Override 
     protected void populateViewHolder(ViewHolder viewHolder, List list, int i) { 

      FragmentBinding binding = viewHolder.getBinding(); 
      binding.setList(list); 
     } 
    }; 
    recyclerView.setAdapter(adapter); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null)); 
    return rootView; 
} 

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

    public FragmentBinding binding; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     binding = DataBindingUtil.bind(itemView); 
     itemView.setOnClickListener(this); 
    } 
    public FragmentBinding getBinding() { 
     return binding; 
    } 

    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(v.getContext(), DetailedActivity.class); 

     **// need help here** 

     v.getContext().startActivity(intent); 
    } 
} 


@BindingAdapter("quantity") 
public static void setQuantityText(TextView view, int quantity) { 
    view.setText(String.valueOf(quantity)); 
} 
public static class Handlers { 

    public static void increment(View view, int max) { 
     FragmentBinding binding = DataBindingUtil.findBinding(view); 
     binding.setQuantity(Math.max(max, binding.getQuantity() + 1)); 
    } 
    public static void decrement(View view, int min) { 
     FragmentBinding binding = DataBindingUtil.findBinding(view); 
     binding.setQuantity(Math.min(min, binding.getQuantity() - 1)); 
    } 
} 

}

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<data> 
    <variable 
     name="List" 
     type="com.xyz.www.android.model.List"/> 

    <variable 
     name="quantity" 
     type="int"/> 

    <variable 
     name="Handlers" 
     type="com.xyz.www.android.ui.fragments.Fragment.Handlers"/> 

</data> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingStart="@dimen/activity_horizontal_margin" 
    android:paddingEnd="@dimen/activity_horizontal_margin" 
    android:paddingTop="8dp"> 

     <ImageView 
      android:layout_width="76dp" 
      android:layout_height="76dp" 
      android:contentDescription="@string/content_description" 
      app:imageUrl="@{List.productImageUrl}" 
      android:padding="8dp" 
      android:layout_gravity="center_horizontal|bottom" 
      android:layout_marginTop="8dp" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="8dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginStart="72dp"> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@{List.productTitle}" 
      android:textSize="16sp" 
      android:textColor="@android:color/primary_text_light" 
      app:font="@{`Roboto-Regular.ttf`}"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@{List.productSku}" 
      android:textSize="13sp" 
      android:paddingTop="8dp" 
      app:font="@{`Roboto-Regular.ttf`}"/> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/Rs" 
       android:textSize="14sp" 
       android:paddingTop="8dp" 
       android:paddingEnd="2dp" 
       android:paddingStart="2dp" 
       app:font="@{`Roboto-Regular.ttf`}"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@{List.productSellingPrice}" 
       android:textSize="14sp" 
       android:paddingTop="8dp" 
       android:paddingStart="2dp" 
       android:paddingEnd="2dp" 
       android:textColor="@android:color/primary_text_light" 
       app:font="@{`Roboto-Regular.ttf`}"/> 

     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignParentEnd="true" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp"> 


     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/decrease" /> 

     <TextView 
      android:layout_width="32dp" 
      android:layout_height="wrap_content" 
      app:font="@{`Roboto-Regular.ttf`}" 
      app:quantity="@{quantity}" 
      android:textAlignment="center" /> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/increase" /> 

    </LinearLayout> 
    </RelativeLayout> 

@JsonIgnoreProperties(ignoreUnknown=true) 

公共類List延伸BaseObservable {

String productTitle; 
String productSku; 
String productImageUrl; 
String productDescription; 
String productMrp; 
String productSellingPrice; 


public List() { 
} 

public List(String productTitle, String productSku, 
       String productImageUrl, String productDescription, 
       String productMrp, String productSellingPrice) { 

    this.productTitle = productTitle; 
    this.productSku = productSku; 
    this.productImageUrl = productImageUrl; 
    this.productDescription = productDescription; 
    this.productMrp = productMrp; 
    this.productSellingPrice = productSellingPrice; 

} 

@Bindable 
public String getProductTitle() { 
    return productTitle; 
} 

@Bindable 
public String getProductSku() { 
    return productSku; 
} 

@Bindable 
public String getProductImageUrl() { 
    return productImageUrl; 
} 

@Bindable 
public String getProductDescription() { 
    return productDescription; 
} 

@Bindable 
public String getProductMrp() { 
    return productMrp; 
} 

@Bindable 
public String getProductSellingPrice() { 
    return productSellingPrice; 
} 
+0

請不要使用類名作爲列表。這不是編碼的好習慣。 –

回答

1

有幾種方法可以做到這一點。一種是製作List Parcelable,這樣你就可以將它作爲一個Intent額外傳遞。然後,您可以提取它並填充詳細信息頁面。

public static final LIST = "ListContent"; 
@Override 
public void onClick(View v) { 
    Intent intent = new Intent(v.getContext(), DetailedActivity.class); 

    FragmentBinding binding = DataBindingUtil.findBinding(v); 
    intent.putExtra(LIST, binding.getList()); 

    v.getContext().startActivity(intent); 
} 

另一種方法是僅傳遞List的ID並再次讀取詳細信息綁定中的信息。該ID添加到列表,然後你就可以提取它,當你啓動活動:

public static final LIST_ID = "ListID"; 
@Override 
public void onClick(View v) { 
    Intent intent = new Intent(v.getContext(), DetailedActivity.class); 

    FragmentBinding binding = DataBindingUtil.findBinding(v); 
    intent.putExtra(LIST_ID, binding.getList().getId()); 

    v.getContext().startActivity(intent); 
} 

無論哪種方式,你可以拉從意向中的數據您的詳細信息活動的OnCreate:

public void onCreate(...) { 
    Intent intent = getIntent(); 
    // Using ID: 
    int id = intent.getIntExtra(LIST_ID); 
    List list = loadListFromId(id); 

    // Using Parcelable: 
    List list = intent.getParcelableExtra(LIST); 
    //... 
}