Custom HeaderView in listView - java

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.

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);
}

RecyclerView item's properites automatically changing while using DataBinding

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.

java.lang.Throwable: addInArray in RecyclerView

Here is the log,
this log shows as many times as the itemView's number.
but my app is still runing normally.
D/ViewGroup﹕ addInArray been called, this = android.support.v7.widget.RecyclerView{44dd4fc8 VFEDID 0,86-720,1140 7f0e01cc app:id/order_query_rv}call stack =
java.lang.Throwable: addInArray
at android.view.ViewGroup.addInArray(ViewGroup.java:3788)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3742)
at android.view.ViewGroup.addView(ViewGroup.java:3566)
at android.view.ViewGroup.addView(ViewGroup.java:3511)
at android.support.v7.widget.RecyclerView$4.addView(RecyclerView.java:538)
at android.support.v7.widget.ChildHelper.addView(ChildHelper.java:83)
at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:6025)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:5983)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:5971)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1373)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1322)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2673)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2971)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15302)
at android.view.ViewGroup.layout(ViewGroup.java:4864)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:817)
at android.view.Choreographer.doCallbacks(Choreographer.java:619)
at android.view.Choreographer.doFrame(Choreographer.java:588)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:803)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit MethodAndArgsCaller.run(ZygoteInit.java:859)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
at dalvik.system.NativeStart.main(Native Method)
I search for a long time ,and i still can't find a way to solve it 。
what cause this Throwable,how to solve it.
Activity
public class OrderQueryActivity extends BaseActivity implements IOrderQueryView {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.base_mains);
super.onCreate(savedInstanceState);
dealTopMenu();
this.iPresenter = new OrderQueryPresenter(OrderQueryActivity.this, new OrderQueryDao());
}
#Override
public void initView() {
super.initView();
final View contentView = View.inflate(getContext(), R.layout.layout_online_order_query, null);
this.orderQueryTitle = (LinearLayout) contentView.findViewById(R.id.order_query_title_ll);
this.addView(contentView);
this.sumNum = (TextView) contentView.findViewById(R.id.order_num_tv);
this.sumWeigh = (TextView) contentView.findViewById(R.id.order_weight);
this.sumAmount = (TextView) contentView.findViewById(R.id.order_total_amount);
this.rv = (RecyclerView) contentView.findViewById(R.id.order_query_recycler_view);
this.rv.setHasFixedSize(true);
this.rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
#Override
public void setAdapterForRv() {
this.adapter = new OrderQueryAdapter(iPresenter);
this.rv.setAdapter(adapter);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/order_query_title_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="vertical"
android:visibility="invisible">
<TextView
style="#style/online_order_query_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingTop="4dp"
android:text="#string/total_order_num" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:paddingLeft="72dp">
<TextView
android:id="#+id/order_num_tv"
style="#style/online_order_query_title_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_number_example" />
<TextView
style="#style/online_order_query_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_number_unit" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/order_weight"
style="#style/online_order_query_title_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_weight_example" />
<TextView
style="#style/online_order_query_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/weight_unit" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/order_total_amount"
style="#style/online_order_query_title_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_total_amount_example" />
<TextView
style="#style/online_order_query_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dp"
android:text="#string/money_unit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/order_query_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
Adapter
public class OrderQueryAdapter extends RecyclerView.Adapter<OrderQueryAdapter.ViewHolder> {
private ArrayList<OrderBean> orderData;
private IOrderQueryPresenter presenter;
public OrderQueryAdapter(IOrderQueryPresenter presenter) {
this.presenter = presenter;
this.orderData = presenter.getOrderData();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.query_online_order_card_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
OrderBean order = orderData.get(position);
holder.dealerName.setText(order.getChannelName());
holder.orderTime.setText(order.getOrderTime());
holder.orderStatus.setText(order.getOrderStatus());
holder.prodsNum.setText(order.getOrderSumNumber());
holder.prodsWeight.setText(order.getWeight());
holder.orderAmount.setText(order.getOrderSumPrice());
holder.payment.setText(order.getPayAmount());
OnDetailClickListener onDetailClickListener = new OnDetailClickListener(presenter, position);
holder.orderContentll.setOnClickListener(onDetailClickListener);
}
#Override
public int getItemCount() {
return orderData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView dealerName, orderTime, orderStatus, prodsNum, prodsWeight, orderAmount, payment;
LinearLayout orderContentll;
public ViewHolder(View view) {
super(view);
this.dealerName = (TextView) view.findViewById(R.id.dealer_name);
this.orderTime = (TextView) view.findViewById(R.id.order_time);
this.orderStatus = (TextView) view.findViewById(R.id.order_status);
this.prodsNum = (TextView) view.findViewById(R.id.prod_count_online_order);
this.prodsWeight = (TextView) view.findViewById(R.id.prod_weight_online_order);
this.orderAmount = (TextView) view.findViewById(R.id.prod_amount_online_order);
this.payment = (TextView) view.findViewById(R.id.payment_order_online);
this.orderContentll = (LinearLayout) view.findViewById(R.id.ll_order_content);
}
}
static class OnDetailClickListener implements View.OnClickListener {
private int position;
private IOrderQueryPresenter presenter;
public OnDetailClickListener(IOrderQueryPresenter presenter, int position) {
this.presenter = presenter;
this.position = position;
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.ll_order_content:
presenter.startOrderDetail(position);
break;
}
}
}
}
item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp">
<android.support.v7.widget.CardView
android:id="#+id/online_pay_card_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="8dp">
<LinearLayout
android:id="#+id/ll_dealer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/dealer_name"
style="#style/online_order_query_primary_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dealer_name" />
<TextView
android:id="#+id/order_time"
style="#style/online_order_query_primary_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:text="#string/order_time_example" />
</LinearLayout>
<ImageView
android:id="#+id/divider_one"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/ll_dealer"
android:background="#drawable/single_divider_line_black"
android:contentDescription="#string/app_name" />
<LinearLayout
android:id="#+id/ll_order_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/divider_one"
android:background="#drawable/order_detail_selector"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_status_str" />
<TextView
android:id="#+id/order_status"
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:text="#string/order_status_example" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="horizontal">
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prod_num_str" />
<TextView
android:id="#+id/prod_count_online_order"
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:text="#string/prod_num_example" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prod_weight" />
<TextView
android:id="#+id/prod_weight_online_order"
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:text="#string/weight_example" />
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/weight_unit" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="horizontal">
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/order_amount" />
<TextView
android:id="#+id/prod_amount_online_order"
style="#style/online_order_query_primary_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:text="#string/order_amount_example" />
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/money_unit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/divider_two"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/ll_order_content"
android:background="#drawable/single_divider_line_black"
android:contentDescription="#string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/divider_two"
android:layout_margin="4dp"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="left"
android:orientation="horizontal">
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/already_payment" />
<TextView
android:id="#+id/payment_order_online"
style="#style/online_order_query_primary_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:text="#string/order_payment_example" />
<TextView
style="#style/size14GreyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/money_unit" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="#+id/payment_detail_btn"
style="#style/iBtn_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mm_title_btn_right"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="#string/payment_detail" />
<Button
android:id="#+id/pay_online_order_btn"
style="#style/iBtn_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="#drawable/mm_title_btn_right"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="#string/pay_online_order" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I had the same problem ...
the problem disappeared after changing my code in fragment
from
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
...
detailAdapter = new TitleAdapter(getChildFragmentManager(),getActivity());//remove this
...
}
on
#Override
public void onAttach(Context context) {
super.onAttach(context);
detailAdapter = new TitleAdapter(getActivity().getSupportFragmentManager (),getActivity());//add this
}

How to create a custom adapter for a listview? Getting RessourceNotFoundException

I am trying to make a custom adapter for my listview, but I keep getting a RessourceNotFoundException on the same line.
Firstly, here is my adapter class code:
public class CreateProgramAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private int layoutResourceId;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TrainingPassHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
holder = new TrainingPassHolder();
convertView.setTag(holder);
} else {
holder = (TrainingPassHolder) convertView.getTag();
}
holder.trainingPass = this.items.get(position);
holder.trainingPassRelativeLayout = (RelativeLayout) row.findViewById(R.id.trainingpass_item_relativelayout);
holder.headlineTxt = (TextView) row.findViewById(R.id.trainingpass_headline);
holder.setNoTxt = (TextView) row.findViewById(R.id.txt_trainingpass_setno);
holder.showHideIcon = (ImageView) row.findViewById(R.id.trainingpass_showhide_imageview);
holder.exerciseSearchField = (EditText) row.findViewById(R.id.trainingpass_exercise_searchfield);
holder.repsTextField = (EditText) row.findViewById(R.id.trainingpass_reps_txtfield);
holder.addSetButton = (Button) row.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) row.findViewById(R.id.trainingpass_addexercise_button);
holder.setsListView = (ListView) row.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) row.findViewById(R.id.trainingpass_exercises_listview);
holder.trainingPassRelativeLayout.setTag(holder.trainingPass);
holder.headlineTxt.setTag(holder.trainingPass);
holder.setNoTxt.setTag(holder.trainingPass);
holder.showHideIcon.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.repsTextField.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
holder.setsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
setupItems(holder);
return convertView;
}
private void setupItems(TrainingPassHolder trainingPassHolder) {
// TODO: set default values for items
}
public static class TrainingPassHolder {
TrainingPass trainingPass;
RelativeLayout trainingPassRelativeLayout;
TextView headlineTxt;
TextView setNoTxt;
ImageView showHideIcon;
EditText exerciseSearchField;
EditText repsTextField;
Button addSetButton;
Button addExerciseButton;
ListView setsListView;
ListView exercisesListView;
}
}
And here is the xml for the view, containing the listview:
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mobileplatformexam.activities.CreateProgramActivity"
android:id="#+id/relativeLayoutCreateProgram">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add training pass"
android:id="#+id/btn_add_trainingpass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btn_add_trainingpass_help"
android:layout_toStartOf="#+id/btn_add_trainingpass_help"
android:onClick="addTrainingPass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="#+id/btn_add_trainingpass_help"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addTrainingPassHelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create program"
android:id="#+id/btn_create_program"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/btn_add_trainingpass_help"
android:layout_alignEnd="#+id/btn_add_trainingpass_help"
android:onClick="createProgram"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView_trainingpasses"
android:layout_above="#+id/btn_create_program"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_add_trainingpass" />
</RelativeLayout>
And the xml file with the item layout for the above listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/trainingpass_item_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="26dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_dark"
android:height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="#+id/trainingpass_showhide_imageview"
android:layout_toStartOf="#+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="#+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_alignBottom="#+id/trainingpass_headline" />
<EditText
android:id="#+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/trainingpass_showhide_imageview"
android:layout_alignEnd="#+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="#android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="#+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="#+id/trainingpass_reps_txtfield"
android:layout_toStartOf="#+id/trainingpass_reps_txtfield" />
<EditText
android:id="#+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="#+id/txt_trainingpass_setno"
android:layout_toLeftOf="#+id/add_set_button"
android:layout_toStartOf="#+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="#+id/trainingpass_reps_txtfield"
android:layout_alignRight="#+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_sets_listview"
android:layout_alignLeft="#+id/trainingpass_exercise_searchfield"
android:layout_alignStart="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_addexercise_button"
android:layout_below="#+id/trainingpass_sets_listview"
android:layout_alignRight="#+id/add_set_button"
android:layout_alignEnd="#+id/add_set_button"
android:layout_alignLeft="#+id/trainingpass_sets_listview"
android:layout_alignStart="#+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_exercises_listview"
android:layout_below="#+id/trainingpass_addexercise_button"
android:layout_alignLeft="#+id/trainingpass_addexercise_button"
android:layout_alignStart="#+id/trainingpass_addexercise_button" />
</RelativeLayout>
</LinearLayout>
And this is the code for setting the adapter in my activity (extends Activity)
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.setTrainingPassName("wefwef");
TrainingPass trainingPass2 = new TrainingPass();
trainingPass.setTrainingPassName("rwefhtr");
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
The error i am getting is this:
12661-12661/com.mobileplatformexam E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f04001a
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.mobileplatformexam.Adapters.CreateProgramAdapter.getView(CreateProgramAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2466)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12807)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:642)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:424)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:690)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218)
at android.view.View.measure(View.java:12807)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
The error refers to line 56 in my adapterclass, which is the following line:
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
I really can't seem to find out what may be wrong with the code, so I hope someone can help me out.
Thanks
Thanks to JafarKhQ, I solved the problem.
By changing the layout "adapter_createprogram_trainingpasses_item .xml" to the following, it worked:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="20dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_dark"
android:height="30dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="#+id/trainingpass_showhide_imageview"
android:layout_toStartOf="#+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="#+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:height="30dp"
android:minWidth="50dp"
android:layout_alignBottom="#+id/trainingpass_headline" />
<EditText
android:id="#+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/trainingpass_showhide_imageview"
android:layout_alignEnd="#+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="#android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="#+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="#+id/trainingpass_reps_txtfield"
android:layout_toStartOf="#+id/trainingpass_reps_txtfield" />
<EditText
android:id="#+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="#+id/txt_trainingpass_setno"
android:layout_toLeftOf="#+id/add_set_button"
android:layout_toStartOf="#+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="#+id/trainingpass_reps_txtfield"
android:layout_alignRight="#+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_sets_listview"
android:layout_alignLeft="#+id/trainingpass_exercise_searchfield"
android:layout_alignStart="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_addexercise_button"
android:layout_below="#+id/trainingpass_sets_listview"
android:layout_alignRight="#+id/add_set_button"
android:layout_alignEnd="#+id/add_set_button"
android:layout_alignLeft="#+id/trainingpass_sets_listview"
android:layout_alignStart="#+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_exercises_listview"
android:layout_below="#+id/trainingpass_addexercise_button"
android:layout_alignLeft="#+id/trainingpass_addexercise_button"
android:layout_alignStart="#+id/trainingpass_addexercise_button" />
</RelativeLayout>

Categories