У меня есть просмотр списка, который я реализовал фрагментом. Я хочу сделать переход к фрагменту сведений в тот момент, когда я нажимаю на один элемент, и я хочу отобразить детали в другом фрагменте, где у меня есть одно изображение для этого изображения и 3 вида текста для имени, цены и деталей. Я перепробовал несколько вариантов, но они не удаются. Большое спасибо
items.java
public class items {
// Name of the object
private String lName;
// Costs
private String lPrice;
// Details
private String lDetails;
// Image resource id
private int lImageId;
// Constructor
public items(String ObjectName,String ObjectPrice,int ImageResourceId,String DetailsItem){
lName=ObjectName;
lPrice=ObjectPrice;
lImageId=ImageResourceId;
lDetails=DetailsItem;
}
// Getters
public String getlName() { return lName; }
public String getlPrice(){ return lPrice; }
public int getlImageId(){ return lImageId; }
public String getlDetails() { return lDetails; }
}
itemsAdapter.java
public class itemsAdapter extends ArrayAdapter<items>{
private static final String LOG_TAG = itemsAdapter.class.getSimpleName();
/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*/
public itemsAdapter(Activity context, ArrayList<items> item){
super(context,0,item);
}
//Provides a view for an AdapterView (ListView, GridView, etc.)
public View getView(int position, View convertView, ViewGroup parent){
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the object located at this posiiton in the list
items currentItems=getItem(position);
// Find the TextView in the list_item.xml layout with the id name_items
TextView nameTextView=(TextView)listItemView.findViewById(R.id.name_item);
// Get the name from the current items object and set this text on the name TextView
nameTextView.setText(currentItems.getlName());
// Find the TextView in the list_item.xml layout with the id price_items
TextView priceTextView=(TextView)listItemView.findViewById(R.id.price_item);
// Get the price from the current items object and set this text on the price TextView
priceTextView.setText(currentItems.getlPrice());
// Find the ImageView in the list_item.xml layout with the id icon_item
ImageView iconImageView=(ImageView)listItemView.findViewById(R.id.icon_item);
// Get the image from the current items object and set this image on the image view
iconImageView.setImageResource(currentItems.getlImageId());
// Find the TextView in the list_item.xml layout with the id details_item
TextView detailsView=(TextView)listItemView.findViewById(R.id.details_item);
// Get the details from the current items object and set this text on the details TextView
detailsView.setText(currentItems.getlDetails());
return listItemView;
}
}
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/icon_item"
android:layout_width="100dp"
android:layout_height="80dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="@+id/name_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textStyle="bold" />
<TextView
android:id="@+id/price_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
android:textStyle="bold" />
<TextView
android:id="@+id/details_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
beds_fragment.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_beds"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:background="#e3e3e3"
/>
bedsFragment.java
public class bedsFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.beds_fragment, container, false);
// Create an ArrayList of items objects
ArrayList<items> beds=new ArrayList<items>();
beds.add(new items("Bed1","23$",R.drawable.bed1,"Beds for sleeping well, best buy if you want to sleep."));
beds.add(new items("Bed2","43$",R.drawable.bed2,"Beds for sleeping well, best buy if you want to sleep."));
beds.add(new items("Bed3","54$",R.drawable.bed3,"Beds for sleeping well, best buy if you want to sleep."));
beds.add(new items("Bed4","34$",R.drawable.bed4,"Beds for sleeping well, best buy if you want to sleep."));
beds.add(new items("Bed5","65$",R.drawable.bed5,"Beds for sleeping well, best buy if you want to sleep."));
// Create an {@link itemsAdapter}, whose data source is a list of {@items}. The adapter
// know how to create a list item views for each item in the list
itemsAdapter itAdapter=new itemsAdapter(this.getActivity(),beds);
// Get a reference to the ListView, and attach the adapter to the listView
ListView listView=(ListView)view.findViewById(R.id.listview_beds);
listView.setAdapter(itAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.listview_beds, new storeFragment());
fragmentTransaction.addToBackStack(null).commit();
}
});
return view;
}
}
detailsFragment.java
public class detailsFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_fragment, container, false);
return view;
}
}