RecyclerView item's properites automatically changing while using DataBinding - java

I'm trying to build a simple task using DataBinding (in Java) without any design pattern.
The scenario is that there is a RecyclerView in which items/ products are binding from a HardCoded List. Whenever a user clicks on any item, it should show that specific item on the Left (blank) side, with default values (properties). Now when the user clicks on that left-side item its properties (quantity, price, etc.) should be changed (only within that left-side box). Now everything is going fine. The problem is that after changing the left-side item's properties, whenever the user again clicks on the same item (that he clicked earlier) it is showing the item with changed values on the left side. But I want that on clicking again it should show the item with default values. I've attached all the classes and screenshots. Kindly identify me If I'm doing anything wrong.
Product.java
public class Product {
private String title;
private String price;
private String image;
private String quantity;
private String discount;
public Product(String title, String price, String image, String quantity, String discount) {
this.title = title;
this.price = price;
this.image = image;
this.quantity = quantity;
this.discount = discount;
}
//Getters and Setters
}
MainTestActivity.java
public class MainTestActivity extends AppCompatActivity implements ProductsListAdapter.ProductItemClickListener{
ActivityMainTestingBinding binding;
private Product myCurrentProduct;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main_testing);
getItems();
}
private void getItems(){
List<Product> products = new ArrayList<>();
products.add(new Product("Black Grapes", "480", "", "1", "0"));
products.add(new Product("Oranges", "198", "", "1", "0"));
products.add(new Product("Pears", "170", "", "1", "0"));
products.add(new Product("Apple", "169", "", "1", "0"));
products.add(new Product("Jonagold", "110", "", "1", "0"));
products.add(new Product("Lemon", "198", "", "1", "0"));
binding.rvProductList.setLayoutManager(new GridLayoutManager(this, 5));
binding.rvProductList.setAdapter(new ProductsListAdapter(products, this));
}
#Override
public void onProductItemClicked(Product product) {
myCurrentProduct = product;
binding.setPopUpProduct(myCurrentProduct);
binding.cvCurrentProduct.setVisibility(View.VISIBLE);
binding.cvCurrentProduct.setOnClickListener(v -> {
myCurrentProduct.setDiscount("100");
myCurrentProduct.setPrice("50000");
myCurrentProduct.setQuantity("5");
myCurrentProduct.setTitle("Null");
binding.setPopUpProduct(myCurrentProduct);
});
}
}
ProductsListAdapter.java
public class ProductsListAdapter extends RecyclerView.Adapter<ProductsListAdapter.ViewHolder> {
private ProductItemClickListener mListener;
private List<Product> products;
public ProductsListAdapter(List<Product> products, ProductItemClickListener mListener) {
this.products = products;
this.mListener = mListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
ItemListProductsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_list_products, viewGroup, false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int i) {
holder.bind(products.get(i), mListener);
}
#Override
public int getItemCount() {
return products.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ItemListProductsBinding binding;
public ViewHolder(ItemListProductsBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(Product currentProduct, ProductItemClickListener clickListener){
//For each item, corresponding product object is passed to the binding
binding.setProduct(currentProduct);
binding.setProductItemClick(clickListener);
//This is to force bindings to execute right away
binding.executePendingBindings();
}
}
public interface ProductItemClickListener {
void onProductItemClicked(Product product);
}
}
activity_main_testing.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="popUpProduct"
type="tech.oratier.parlour.models.Product" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cvCurrentProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher_round" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:hint="Product Title"
android:text="#{popUpProduct.title}"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:text="Quantity:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:gravity="end"
android:text="#{popUpProduct.quantity}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:text="Discount:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:gravity="end"
android:text="#{popUpProduct.discount}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:text="Price:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:gravity="end"
android:text="#{popUpProduct.price}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:text="Total"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/garamond"
android:gravity="end"
android:text="#{popUpProduct.price != null ? (popUpProduct.quantity != null ? (popUpProduct.discount != null ? String.valueOf((Double.parseDouble(popUpProduct.price) * Double.parseDouble(popUpProduct.quantity)) - Double.parseDouble(popUpProduct.discount)) : String.valueOf(Double.parseDouble(popUpProduct.price) * Double.parseDouble(popUpProduct.quantity))) : #string/nothing) : #string/nothing}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<View
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="#color/seperator" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvProductList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
item_list_products.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="product"
type="tech.oratier.parlour.models.Product" />
<variable
name="productItemClick"
type="tech.oratier.parlour.adapters.ProductsListAdapter.ProductItemClickListener" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:onClick="#{()->productItemClick.onProductItemClicked(product)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:contentPaddingRight="5dp"
android:orientation="vertical">
<LinearLayout
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="wrap_content">
<ImageView
android:layout_marginTop="8dp"
android:src="#mipmap/ic_launcher_round"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:fontFamily="#font/garabd"
android:paddingLeft="8dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:background="#color/priceBackground"
android:textColor="#color/defaultTextColor"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="#{product.price}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<com.google.android.material.textview.MaterialTextView
android:singleLine="true"
android:textSize="20sp"
android:textColor="#android:color/black"
android:fontFamily="#font/raleway_regular"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:text="#{product.title}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.textview.MaterialTextView
android:singleLine="true"
android:textSize="20sp"
android:textColor="#android:color/black"
android:fontFamily="#font/raleway_regular"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:text="#{product.quantity}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

The problem is that I'm using
myCurrentProduct = product;
Here it binds myCurrentProduct with product / layout therefore its value automatically updating.
So, to get rid of this I declared another object tempProduct as,
tempProduct = product;
then I get values from tempProduct
myCurrentProduct = new Product(); //create a default constructor in Product model class
myCurrentProduct.setTitle(tempProduct.getTitle());
myCurrentProduct.setQuantity("5");
myCurrentProduct.setPrice("50000");
myCurrentProduct.setDiscount(tempProduct.getDiscount());
binding.setPopUpProduct(myCurrentProduct);
In this way I'm also able to get values from the clicked item, also set my own values.

Related

why is my recycler view only showing 1 card

the app is used to sign up for events. Now I am unable to display more then one card in the recycler view even though there is data in the database.it only would display only the first row of data. I know this as I have deleted the first row and the app shows the new first row.
The php works as i have loaded the page and it gives me the data from the database.
recycler view activity
public class MainActivity2 extends AppCompatActivity {
private static final String URL = "http://10.0.2.2/mad/activitydisplay.php";
RecyclerView recyclerView;
UserAdapter userAdapter;
List<Users> usersList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView = findViewById(R.id.recylcerList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
usersList=new ArrayList<>();
LoadAllUser();
}
private void LoadAllUser() {
JsonArrayRequest request =new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray array) {
for (int i=0;i<array.length();i++){
try {
JSONObject object=array.getJSONObject(i);
String name=object.getString("eventname").trim();
String type=object.getString("type").trim();
String location=object.getString("location").trim();
String time=object.getString("time").trim();
String date=object.getString("date").trim();
String maxparticipants=object.getString("maxparticipants").trim();
Users user= new Users();
user.setName(name);
user.setType(type);
user.setLocation(location);
user.setTime(time);
user.setDate(date);
user.setMaxparticipants(maxparticipants);
usersList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
userAdapter=new UserAdapter(MainActivity2.this,usersList);
recyclerView.setAdapter(userAdapter);
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue=Volley.newRequestQueue(MainActivity2.this);
requestQueue.add(request);
}
}
recycler view adpater
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
Context context;
List<Users> usersList;
public UserAdapter(Context context, List<Users> usersList) {
this.context = context;
this.usersList = usersList;
}
#NonNull
#Override
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list,parent,false);
return new UserHolder(userLayout);
}
#Override
public void onBindViewHolder(#NonNull UserHolder holder, int position) {
Users users=usersList.get(position);
holder.Name_id.setText(users.getName());
holder.Type_id.setText(users.getType());
holder.Location_id.setText(users.getLocation());
holder.Time_id.setText(users.getTime());
holder.Date_id.setText(users.getDate());
holder.Slots_id.setText(users.getMaxparticipants());
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, cardviewclick.class);
intent.putExtra("Name",holder.Name_id.getText());
intent.putExtra("Type",holder.Type_id.getText());
intent.putExtra("Location",holder.Location_id.getText());
intent.putExtra("Time",holder.Time_id.getText());
intent.putExtra("Date",holder.Date_id.getText());
intent.putExtra("Slots",holder.Slots_id.getText());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return usersList.size();
}
public class UserHolder extends RecyclerView.ViewHolder{
TextView Name_id,Type_id,Location_id,Time_id,Date_id,Slots_id;
CardView cardView;
public UserHolder(#NonNull View itemView){
super(itemView);
Name_id=itemView.findViewById(R.id.textTitle);
Type_id=itemView.findViewById(R.id.textType);
Location_id=itemView.findViewById(R.id.textLocation);
Time_id=itemView.findViewById(R.id.textTime);
Date_id=itemView.findViewById(R.id.textDate);
Slots_id=itemView.findViewById(R.id.textSlots);
cardView=itemView.findViewById(R.id.card);
}
}
}
activitydisplay.php
conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("SELECT eventname, type ,location,maxparticipants,time,date FROM activitytable");
$stmt ->execute();
$stmt -> bind_result($eventname, $type, $location,$maxparticipants,$time,$date);
$data= array();
while($stmt ->fetch()){
$temp = array();
$temp['eventname'] = $eventname;
$temp['type'] = $type;
$temp['location'] = $location;
$temp['maxparticipants'] = $maxparticipants;
$temp['time'] = $time;
$temp['date'] = $date;
array_push($data,$temp);
}
echo json_encode($data);
?>
edit 1:add xml code
main activity 2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recylcerList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
card xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/card"
app:cardElevation="12dp"
app:cardCornerRadius="16dp"
android:layout_margin="16dp"
android:backgroundTint="#efefef"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textTitle"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textType"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textLocation"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textDate"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textTime"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slots:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textSlots"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
It is because the LinearLayout of the card has match_parent as its layout_height and layout_width so it is taking up the whole space. Can you try to scroll the card up?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent" <-------- here
android:layout_height="match_parent">. <-------- here
The layout_height should be wrap_content.

How to retrieve RecyclerView position

I am trying to get the position of items in my RecyclerView, mainly those that are at [0], [1], and [2] positions. The reason being is currently I am creating a leader board that will highlight the top 3 players with the highest score.
I have tried calling getAdapterPosition() but I guess I am using it incorrectly.
if(holder.getAdapterPosition() == 0)
{
Glide.with(holder.firstPlace.getContext())
.load(model.getUrl())
.into(holder.firstPlace);
}
I would greatly appreciate if someone could point me to the right direction. Thank you.
Edit 1:
My UserAdapter
public class UserAdapter extends FirestoreRecyclerAdapter<UserModel, UserAdapter.UserViewHolder> {
public UserAdapter(#NonNull FirestoreRecyclerOptions<UserModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull UserAdapter.UserViewHolder holder, int position, #NonNull UserModel model) {
holder.username.setText(model.getFullName());
holder.email.setText(model.getEmail());
holder.score.setText(model.getScore()+"");
holder.rank.setText(String.valueOf(position + 1));
Glide.with(holder.userImage.getContext())
.load(model.getUrl())
.into(holder.userImage);
//error here
// if(holder.getAdapterPosition() == 0)
// {
// Glide.with(holder.firstPlace.getContext())
// .load(model.getUrl())
// .into(holder.firstPlace);
// }
}
#NonNull
#Override
public UserAdapter.UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_leaderboard_single, parent, false);
return new UserViewHolder(view);
}
public class UserViewHolder extends RecyclerView.ViewHolder {
CircleImageView userImage;
TextView username;
TextView email;
TextView score;
TextView rank;
CircleImageView firstPlace;
CircleImageView secondPlace;
CircleImageView thirdPlace;
public UserViewHolder(#NonNull View itemView) {
super(itemView);
userImage = itemView.findViewById(R.id.list_image);
username = itemView.findViewById(R.id.list_username);
email = itemView.findViewById(R.id.list_email);
score = itemView.findViewById(R.id.list_score);
rank = itemView.findViewById(R.id.leaderboard_position);
firstPlace = itemView.findViewById(R.id.place1stProfile);
secondPlace = itemView.findViewById(R.id.place2ndProfile);
thirdPlace = itemView.findViewById(R.id.place3rdProfile);
}
}
list_leaderboard_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:background="#EFCDB4">
<RelativeLayout
android:layout_width="32dp"
android:layout_height="match_parent"
android:padding="5dp"
android:background="#FFE5C6"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/leaderboard_position"
android:textStyle="bold"
android:textColor="#000"
android:text="##"
android:layout_centerInParent="true"
android:textSize="18sp"
/>
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="5dp"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/list_image"
android:src="#mipmap/ic_launcher_round"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#id/leaderboard_position"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/list_username"
android:layout_width="173dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppinsmedium"
android:layout_toRightOf="#id/list_image"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Username"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:id="#+id/list_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_toRightOf="#+id/list_username"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:textColor="#000"
android:visibility="gone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_marginTop="0dp"
android:id="#+id/list_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="25sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PTS"
android:textStyle="bold"
android:fontFamily="#font/poppinsbold"
android:textSize="15sp"
android:textColor="#000"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_blood_drop"
android:visibility="gone"
/>
</LinearLayout>
fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Leaderboard"
android:background="#790604">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="L E A D E R B O A R D"
android:fontFamily="#font/poppinsbold"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginBottom="16dp"
android:minHeight="150dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place2ndProfile"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_below="#id/place2ndProfile"
android:fontFamily="#font/poppinsbold"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:textColor="#ffffff"
android:text="2ND"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place1stProfile"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:fontFamily="#font/poppinsbold"
android:layout_below="#id/place1stProfile"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="1ST"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place3rdProfile"
android:src="#mipmap/ic_launcher_round"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:fontFamily="#font/poppinsbold"
android:layout_below="#id/place3rdProfile"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="3RD"/>
</RelativeLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/leaderboard_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
You should call holder.getAdapterPosition only within the ViewHolder; instead you can simply use the parameter position passed in onBindViewHolder().
#Override
protected void onBindViewHolder(#NonNull UserAdapter.UserViewHolder holder, int position, #NonNull UserModel model) {
// .....
if(position == 0)
{
Glide.with(holder.firstPlace.getContext())
.load(model.getUrl())
.into(holder.firstPlace);
}

Android fragment oncreateview is called but view is not inflated and there are no errors

So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.
Here is my swapFragment() method that calls my fragment:
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flgenre, genresFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Here is my GenresFragment.java:
public class GenresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private DatabaseReference mPostReference;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
private ArrayList<String> genreList = new ArrayList<>();
private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public GenresFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static GenresFragment newInstance(int columnCount) {
GenresFragment fragment = new GenresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);
if (rootView == null){
Log.e("TEST1", "Is Null");
}else {
Log.e("TEST2", "Not Null");
}
return rootView;
}
public void failure(){
Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(String item);
}
}
And my fragment_mini_add.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
tools:context="com.nocrat.fanti.ProfileActivity"
android:id="#+id/flgenre">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="#string/add_new_project"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/rLayout1"
android:layout_below="#id/textView4">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/project_name"
android:gravity="center_vertical"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:hint="#string/group_name"
android:inputType="textPersonName"
android:layout_below="#id/editText"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_below="#id/rLayout1">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create"
android:textColor="#FFD700"
android:textSize="18sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:textColor="#FFD700"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here is my onClickListener that calls swapFragment():
addGenre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
swapFragment();
}
});
Here is my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:background="#ffffff"
android:orientation="vertical"
android:id="#+id/flgenre">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:elevation="40dp"
android:background="#ffffff">
<include
layout="#layout/app_bar_profile"
android:layout_height="190dp"
android:layout_width="match_parent"
android:id="#+id/app1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8musicalnotes64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Genres"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rounded_genres"
android:gravity="center"
android:clickable="true"
android:id="#+id/addGenres">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_add_white_48px" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8resume64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Bio" />
<EditText
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Tell us about yourself..."
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="edit"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8trophy64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Average Rank"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8meeting64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Collaborations"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.
Try to modify you swapFragment() with below code by replacing replace() with add()
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flgenre, genresFragment);
fragmentTransaction.commit();
}
Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.
Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.
Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)
Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.
You should remove android:id="#+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml.
replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml.
then, add this code to your activity xml to contain the fragment
<FrameLayout
android:id="#+id/flgenre"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Custom HeaderView in listView

I have 2 views for an item ListView. So one is used as HeaderView for ListView and the other in the other. That is the second marking takes the data from the DetailsAdapter, which respectively are initialized fields.
public class DetailsAdapter extends ArrayAdapter<TicketObjects> {
private int resource;
private LayoutInflater inflater;
private Context context;
public DetailsAdapter ( Context ctx, int resourceId, List<TicketObjects> objects) {
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = (LinearLayout) inflater.inflate( resource, null );
TicketObjects ticketObjects = getItem( position );
TextView depTransferCity = (TextView) convertView.findViewById(R.id.tvTransferCity);
TextView detDepartTime = (TextView) convertView.findViewById(R.id.detDepartTime);
TextView detDepartDate = (TextView) convertView.findViewById(R.id.detDepartDate);
TextView detArriveTime = (TextView) convertView.findViewById(R.id.detArriveTime);
TextView detArriveDate = (TextView) convertView.findViewById(R.id.detArriveDate);
TextView depDepartCity = (TextView) convertView.findViewById(R.id.depDepartCity);
TextView detDepartAirport = (TextView) convertView.findViewById(R.id.detDepartAirport);
TextView detArriveCity = (TextView) convertView.findViewById(R.id.detArriveCity);
TextView detArriveAirport = (TextView) convertView.findViewById(R.id.detArriveAirport);
TextView detFlight = (TextView) convertView.findViewById(R.id.detFlight);
ImageView airlineLogo = (ImageView) convertView.findViewById(R.id.detAirportLogo);
depTransferCity.setText(ticketObjects.getTransferCity());
detDepartTime.setText(ticketObjects.getDepartTime());
detDepartDate.setText(ticketObjects.getDepartDate());
detArriveTime.setText(ticketObjects.getArriveTime());
detArriveDate.setText(ticketObjects.getArriveDate());
depDepartCity.setText(ticketObjects.getDepartCity());
detDepartAirport.setText(ticketObjects.getDepartAirport());
detArriveCity.setText(ticketObjects.getArriveCity());
detArriveAirport.setText(ticketObjects.getArriveAirport());
detFlight.setText(ticketObjects.getFlight());
airlineLogo.setImageResource(ticketObjects.getAirlineLogo());
return convertView;
}
}
MainActivity:
public class MainActivity extends Activity {
private ListView lvDetails;
private Context ctx;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket_details);
ctx=this;
List<TicketObjects> ticketObjectses = new ArrayList<>();
ticketObjectses.add(new TicketObjects("Бишкек", "Манас", "Ош", "Аэропорт Оша", "FRU", "13:45", "16.09.2015", "OSS", "13:45", "16.09.2015", "Almaty", "1ч 15мин", "15000", R.drawable.logo_flyduba, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Ош", "Аэропорт Оша", "Новосибирск", "Толмачево", "OSS", "15:43", "16.09.2015", "OVB", "17:45", "16.09.2015", "Astana", "1ч 25мин", "16000", R.drawable.logo_kazak, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Алматы", "Алматы", "Москва", "Домодедово","ALA","11:54","16.09.2015","DME","12:44","16.09.2015","Novosibirsk", "2ч 15мин","13000", R.drawable.logo_pegasus_logo, "Рейс: 543"));
lvDetails = ( ListView ) findViewById( R.id.lvDetails);
ViewGroup header = (ViewGroup) getLayoutInflater().inflate(R.layout.ticket_details_header_item,lvDetails,false);
lvDetails.addHeaderView(header);
lvDetails.setAdapter(new DetailsAdapter(ctx, R.layout.ticket_details_item, ticketObjectses));
}
}
ticket_details_item_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/leftPadding"
android:layout_below="#+id/line"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout"
android:background="#drawable/layouts_border">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/iconsSize"
android:layout_gravity="center_horizontal"
android:padding="#dimen/leftPadding"
android:background="#e8e8e8"
android:layout_marginBottom="#dimen/rightPadding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="#+id/detTitleArriveCity"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/imageView"
android:layout_toStartOf="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Нью-Йорк"
android:id="#+id/detTitleArriveCity"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView" />
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/icon_depart"
android:padding="#dimen/rightPadding"
android:layout_marginLeft="#dimen/rightPadding"
android:layout_marginRight="#dimen/rightPadding" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35ч 00м"
android:id="#+id/detTotalDuration"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainSmallSize"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/leftPadding"
android:gravity="center_vertical"
android:paddingLeft="#dimen/text_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="3"
android:gravity="center">
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView2"
android:src="#drawable/from"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView3"
android:src="#drawable/punktir"
android:layout_below="#+id/imageView2"
android:layout_marginTop="-5dp" />
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView4"
android:src="#drawable/to"
android:layout_below="#+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09:50"
android:id="#+id/detDepartTime"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.06.2016"
android:id="#+id/detDepartDate"
android:layout_below="#+id/detDepartTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="#dimen/rightPadding"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11:40"
android:id="#+id/detArriveTime"
android:layout_below="#+id/detDepartDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13.06.2016"
android:id="#+id/detArriveDate"
android:layout_below="#+id/detArriveTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="2"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="#+id/depDepartCity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Манас"
android:id="#+id/detDepartAirport"
android:layout_below="#+id/depDepartCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="#dimen/rightPadding"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Москва"
android:id="#+id/detArriveCity"
android:layout_below="#+id/detDepartAirport"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Шереметьево"
android:id="#+id/detArriveAirport"
android:layout_below="#+id/detArriveCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/stndHeight"
android:layout_gravity="center_horizontal"
android:paddingTop="#dimen/leftPadding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Рейс: AN565"
android:id="#+id/detFlight"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detAirportLogo"
android:src="#drawable/logo_flyduba"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ticket_details_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/leftPadding"
android:background="#e8e8e8"
android:gravity="center_vertical">
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/locIcon"
android:src="#drawable/location"
android:layout_weight="1"
android:padding="#dimen/leftPadding" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Пересадка:"
android:id="#+id/textviewTransfer"
android:layout_weight="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvTransferCity"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize"
android:layout_weight="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/leftPadding"
android:gravity="center_vertical"
android:paddingLeft="#dimen/text_margin"
android:id="#+id/centerlayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="3"
android:gravity="center">
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView2"
android:src="#drawable/from"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView3"
android:src="#drawable/punktir"
android:layout_below="#+id/imageView2"
android:layout_marginTop="-5dp" />
<ImageView
android:layout_width="#dimen/smallIconSize"
android:layout_height="#dimen/smallIconSize"
android:id="#+id/imageView4"
android:src="#drawable/to"
android:layout_below="#+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09:50"
android:id="#+id/detDepartTime"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.06.2016"
android:id="#+id/detDepartDate"
android:layout_below="#+id/detDepartTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="#dimen/rightPadding"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11:40"
android:id="#+id/detArriveTime"
android:layout_below="#+id/detDepartDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13.06.2016"
android:id="#+id/detArriveDate"
android:layout_below="#+id/detArriveTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/rightPadding"
android:layout_weight="2"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="#+id/depDepartCity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Манас"
android:id="#+id/detDepartAirport"
android:layout_below="#+id/depDepartCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="#dimen/rightPadding"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Москва"
android:id="#+id/detArriveCity"
android:layout_below="#+id/detDepartAirport"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Шереметьево"
android:id="#+id/detArriveAirport"
android:layout_below="#+id/detArriveCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/mainGreyColor"
android:textSize="#dimen/mainMiddleSize" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/stndHeight"
android:layout_gravity="center_horizontal"
android:paddingTop="#dimen/leftPadding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Рейс: AN565"
android:id="#+id/detFlight"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="#color/otherTextColor"
android:textSize="#dimen/mainLargeSize" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/detAirportLogo"
android:src="#drawable/logo_flyduba"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
Now ticket_details_header_item.xml nothing displays, that is, there are no data. A second view As you can see in the adapter is initialized and it has data.
DetailObjects
package kz.ticketdetail;
import java.text.DecimalFormat;
public class TicketObjects {
private String departCity;
private String departAirport;
private String arriveCity;
private String arriveAirport;
private String departCode;
private String departTime;
private String departDate;
private String arriveCode;
private String arriveTime;
private String arriveDate;
private String transferCity;
private String flyDuration;
private String ticketPrice;
private int airlineLogo;
private String flight;
public TicketObjects(String departCity, String departAirport, String arriveCity, String arriveAirport,
String departCode, String departTime, String departDate,
String arriveCode, String arriveTime, String arriveDate,
String transferCity, String flyDuration, String ticketPrice, int airlineLogo, String flight) {
this.departCity = departCity;
this.departAirport = departAirport;
this.arriveCity = arriveCity;
this.arriveAirport = arriveAirport;
this.departCode = departCode;
this.departTime = departTime;
this.departDate = departDate;
this.arriveCode = arriveCode;
this.arriveTime = arriveTime;
this.arriveDate = arriveDate;
this.transferCity = transferCity;
this.flyDuration = flyDuration.trim().replaceFirst("^[0]{1}", "").replace(":", " h ") + " m";
this.ticketPrice = ticketPrice;
this.airlineLogo = airlineLogo;
this.flight = flight;
}
public static String getFormattingPrice(final String ticketPrice) throws IllegalArgumentException {
DecimalFormat formatter = new DecimalFormat("#,###,###");
return formatter.format(Float.parseFloat(ticketPrice)).replace(",", " ");
}
public String getDepartCity(){
return departCity;
}
public void setDepartCity(String departCity){
this.departCity = departCity;
}
public String getDepartAirport(){
return departAirport;
}
public void setDepartAirport(String departAirport){
this.departAirport = departAirport;
}
public String getArriveCity(){
return arriveCity;
}
public void setArriveCity(String arriveCity){
this.arriveCity = arriveCity;
}
public String getArriveAirport(){
return arriveAirport;
}
public void setArriveAirport(String arriveAirport){
this.arriveAirport = arriveAirport;
}
public String getFlight(){
return flight;
}
public void setFlight(String flight){
this.flight = flight;
}
public String getDepartCode(){
return departCode;
}
public void setDepartCode(String departCode){
this.departCode = departCode;
}
public String getDepartTime(){
return departTime;
}
public void setDepartTime(String departTime){
this.departTime = departTime;
}
public String getDepartDate(){
return departDate;
}
public void setDepartDate(String departDate){
this.departDate = departDate;
}
public String getArriveCode(){
return arriveCode;
}
public void setArriveCode(String arriveCode){
this.arriveCode = arriveCode;
}
public String getArriveTime(){
return arriveTime;
}
public void setArriveTime(String arriveTime){
this.arriveTime = arriveTime;
}
public String getArriveDate(){
return arriveDate;
}
public void setArriveDate(String arriveDate){
this.arriveDate = arriveDate;
}
public String getTransferCity(){
return transferCity;
}
public void setTransferCity(String transferCity){
this.transferCity = transferCity;
}
public String getFlyDuration(){
return flyDuration;
}
public void setFlyDuration(String flyDuration){
this.flyDuration = flyDuration;
}
public String getTicketPrice(){
return ticketPrice;
}
public void setTicketPrice(String ticketPrice){
this.ticketPrice = ticketPrice;
}
public int getAirlineLogo(){
return airlineLogo;
}
public void setAirlineLogo(int airlineLogo){
this.airlineLogo = airlineLogo;
}
}
Question: How header view display the data from the array which MainActivity? Where should occur initialization ticket_details_header_item?
You need to get the view from header and set data like this.
public class MainActivity extends Activity {
private ListView lvDetails;
private Context ctx;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket_details);
ctx=this;
List<TicketObjects> ticketObjectses = new ArrayList<>();
ticketObjectses.add(new TicketObjects("Бишкек", "Манас", "Ош", "Аэропорт Оша", "FRU", "13:45", "16.09.2015", "OSS", "13:45", "16.09.2015", "Almaty", "1ч 15мин", "15000", R.drawable.logo_flyduba, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Ош", "Аэропорт Оша", "Новосибирск", "Толмачево", "OSS", "15:43", "16.09.2015", "OVB", "17:45", "16.09.2015", "Astana", "1ч 25мин", "16000", R.drawable.logo_kazak, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Алматы", "Алматы", "Москва", "Домодедово","ALA","11:54","16.09.2015","DME","12:44","16.09.2015","Novosibirsk", "2ч 15мин","13000", R.drawable.logo_pegasus_logo, "Рейс: 543"));
lvDetails = ( ListView ) findViewById( R.id.lvDetails);
View header = getLayoutInflater().inflate(R.layout.ticket_details_header_item,lvDetails,false);
TextView text1 = (TextView) header.findViewById(R.id.detTitleArriveCity);
TextView text2 = (TextView) header.findViewById(R.id.detTotalDuration);
// do the same far all your text views or what ever you want to get from layout.
TicketObjects mTicketObjects = ticketObjectses.get(0);
text1.setText(mTicketObjects.getDepartCity());
text2.setText(mTicketObjects.getFlyDuration());
// do the same for other options.
lvDetails.addHeaderView(header);
lvDetails.setAdapter(new DetailsAdapter(ctx, R.layout.ticket_details_item, ticketObjectses));
}
}
do this for all items. inside the activity not in adapter. hope that answers your question.

by clicking on TextView i want to see the list of items

In my android application, i want to display a list of items when i click on a Textview, it display a list of items and i can add and delete items from that list. how can i do it through java code
Kindly guide. i will be very thankful to you
my code is:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cus_name"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/cus_name_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/contact_no"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/contact_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ticket_no"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ticket_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="#string/task_detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/task_detail_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
If I understood right I would suggest extending a Dialog to make custom dialog that pops out whenever user clicks on TextView (as included in Imran Khan's answer). This dialog would contain ListView and whatever else you need for handling the list. Example of such approach as I used it some time ago:
public class LogOverlay extends Dialog{
private Server mItem;
private boolean end=false;
private int mLimitHigh = 15;
private int mLimitLow = 0;
private ListView mListView;
private LogListAdapter mAdapter;
private ArrayList<LogUnit> mLog = new ArrayList<LogUnit>();
private boolean mScroll;
private Context context;
ProgressDialog pd;
public LogOverlay(Context context,Session session,Server server) {
super(context);
this.setContentView(R.layout.overlay_logs);
mListView=(ListView) this.findViewById(R.id.log_list);
mAdapter = new LogListAdapter(context, new ArrayList<LogUnit>(), new String[] {});
mListView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
mListView.setDividerHeight(1);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
....
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}

Categories