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.
Related
I have 2 layouts where first layout for Fragment and second layout for RecyclerView. I have created CardView in RecyclerView, every CardView has some data. And I have created button below RecyclerView where the function of button for sending Data on CardView. My problem is I don't know how to send data when i click button in Fragment.
Fragment Layout :
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/container_daftar_alamat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.menu.menu_signin.menu.MenuAddressFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMoveTambahAlamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#null"
android:drawableStart="#drawable/ic_tambah_alamat"
android:drawablePadding="10dp"
android:text="Tambah Alamat"
android:textAllCaps="false"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Daftar Alamat"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tambah_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/gunakanAlamat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="100dp"
android:background="#drawable/bg_button_red"
android:text="Gunakan Alamat"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
RecyclerView Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
app:cardBackgroundColor="#color/colorWhite"
app:cardCornerRadius="4dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/nameUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Muhammad Rafi Bahrur Rizki"
android:textColor="#color/colorBlack"
android:textSize="14sp"
android:textStyle="bold"
tools:ignore="UnusedAttribute" />
<TextView
android:id="#+id/addressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:maxLines="1"
android:text="(Alamat Kantor)"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/streetName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Mangga Dua Square Lantai 1 Jakarta,"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp"
tools:ignore="UnusedAttribute" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/blokAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="Blok C no. 148 - 150"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/cityAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/countryAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/phoneUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="0812951825"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<Button
android:id="#+id/buttonEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:drawableStart="#drawable/ic_edit"
android:text="Edit"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#id/buttonEdit"
android:drawableStart="#drawable/ic_delete"
android:text="Hapus"
android:textAllCaps="false"
android:textSize="12sp" />
</RelativeLayout>
</TableLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When i select RadioButton, only data in RadioButton will be obtained. And when i click button, the data will be send to another activity.
Adapter RecyclerView :
public class AdapterGetAddress extends RecyclerView.Adapter<AdapterGetAddress.ViewHolder> {
Context context;
private List<ModelGetAddress> modelGetAddressList;
private BaseApiService baseApiService;
private int previousSelected = -1;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_address, parent, false);
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final ModelGetAddress adapterAddress = modelGetAddressList.get(position);
}
#Override
public int getItemCount() {
return modelGetAddressList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameUser, addressUser, streetName, blokAddressUser, cityAddressUser, countryUser, phoneUser;
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameUser = itemView.findViewById(R.id.nameUser);
addressUser = itemView.findViewById(R.id.addressUser);
streetName = itemView.findViewById(R.id.streetName);
countryUser = itemView.findViewById(R.id.countryAddressUser);
blokAddressUser = itemView.findViewById(R.id.blokAddressUser);
cityAddressUser = itemView.findViewById(R.id.cityAddressUser);
phoneUser = itemView.findViewById(R.id.phoneUser);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
}
});
}
}
}
Code in Fragment :
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here's were i'm stuck
}
});
return view;
}
}
You can solve this via an interface.
public interface ItemClickedCallback{
void onItemClicked(String nameUser //, the other data you wanna pass );
}
pass it through the constructor of your adapter.
Use this adapter constructor
ItemClickedCallback callback;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList, ItemClickedCallback callback) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
this.callback= callback;
}
When checking a radio button of any item; call the interface method, like this.
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
callback.onItemClicked(getNameUser //, the rest..);
}
});
Now, when you initialize the adapter in the fragment, create the new interface via the constructor and assign the data to class variables like this.
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
private String nameUser_;
// the rest...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
AdapterGetAddress adapter = new AdapterGetAddress(getActivity, list, new ItemClickedCallback() {
#Override
public void onItemClicked(String userName //, the rest..) {
// assign to the class variable like this
nameUser_ = userName;
// the rest..
}
}););
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Use the data as you want..
}
});
return view;
}
}
Recycler View is only occupying 1st half of the screen no matter what I do.
Can anyone suggest how to make Recyclerview occupy full width of the screen?
This is what it displays me in Android Studio
But this is the actual output on a device. It's all wrapped to the left side of the device.
activity.java
public class RecordLogs extends AppCompatActivity {
private RecyclerView recyclerView;
private RecordsAdapter adapter;
private List<Record> recordList;
private ProgressBar progressBar;
private FirebaseFirestore db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_logs);
progressBar = findViewById(R.id.progressbar);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager((this)));
recordList = new ArrayList<>();
adapter = new RecordsAdapter(this, recordList);
recyclerView.setAdapter(adapter);
db = FirebaseFirestore.getInstance();
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".RecordLogs">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="26/7/2018" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Qty. L"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="25" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rate ₹"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="125" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total ₹"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
adapter.java
public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.RecordViewHolder> {
private Context mCtx;
private List<Record> recordList;
public RecordsAdapter(Context mCtx, List<Record> recordList) {
this.mCtx = mCtx;
this.recordList = recordList;
}
#NonNull
#Override
public RecordViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.record_logs_card, null);
return new RecordViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecordViewHolder holder, int position) {
Record record = recordList.get(position);
holder.date.setText(record.getDate());
holder.qty.setText(String.valueOf(record.getQty()));
holder.rate.setText(String.valueOf(record.getRate()));
holder.total.setText(String.valueOf(record.getTotal()));
}
#Override
public int getItemCount() {
return recordList.size();
}
class RecordViewHolder extends RecyclerView.ViewHolder {
TextView date, qty, rate, total;
public RecordViewHolder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date);
qty = itemView.findViewById(R.id.qty);
rate = itemView.findViewById(R.id.rate);
total = itemView.findViewById(R.id.total);
}
}
}
I found it:
All I had to change the following line of code in my custom adapter as follows:
View view = inflater.inflate(R.layout.record_logs_card, null);
to
View view = inflater.inflate(R.layout.record_logs_card, parent, false);`
This question is been asked multiple times, but everyone has his specific usage of listview adapters (ArrayAdapter or BaseAdapter).
I searched a lot before asking this question, and this same problem happened to me before and i solved it using Holder class, and cleaning code to make it the minimum possible.
But now, i am creating another interface which contains a listview and needs a BaseAdapter to fill it using a List of Object (List).
The weird thing in my case is, if my listview contains only 4 items ( 4 rows ) then everything is ok, but if it contains more than 4, first items will be duplicated, and if i scroll down/up fast , items got wrong positions.
Here is my code :
public class OrdersAdapter extends BaseAdapter {
List<Order> ordersList;
FragmentActivity activity;
private int height;
public OrdersAdapter(FragmentActivity activity, List<Order> ordersList){
this.activity = activity;
this.ordersList = ordersList;
height = Constants.getOneThirdScreenHight(activity);
}
#Override
public int getCount() {
return ordersList.size();
}
#Override
public Order getItem(int position) {
return ordersList.get(position);
}
#Override
public long getItemId(int position) {
return ordersList.hashCode();
}
private class MyHolder{
TextView orderNumber, orderDate, menuCount, orderPrice, orderStatus, orderTelephone, orderStoreName;
ImageView orderDelete, menuPicture;
Order order, mOrder;
String numberText = activity.getResources().getString(R.string.order_number);
HashMap<String, String> map;
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
}
#Override
public View getView (final int position, View convertView, ViewGroup parent) {
View vi;
final MyHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new MyHolder();
vi = layoutInflater.inflate(R.layout.sent_order_item, null);
holder.order = getItem(position);
holder.mOrder = new Order();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.height = height;
vi.findViewById(R.id.order_history_layout).setLayoutParams(layoutParams);
holder.orderNumber = (TextView) vi.findViewById(R.id.order_number);
holder.orderDate = (TextView) vi.findViewById(R.id.order_date);
holder.orderDelete = (ImageView) vi.findViewById(R.id.order_delete);
holder.orderPrice = (TextView) vi.findViewById(R.id.order_history_price);
holder.orderStatus = (TextView) vi.findViewById(R.id.order_history_status);
holder.orderStoreName = (TextView) vi.findViewById(R.id.order_history_store);
holder.orderTelephone = (TextView) vi.findViewById(R.id.order_history_phone);
holder.menuCount = (TextView) vi.findViewById(R.id.order_menu_count1) ;
holder.menuPicture = (ImageView) vi.findViewById(R.id.order_picture);
vi.setTag(holder);
}else{
vi = convertView;
holder = (MyHolder) vi.getTag();
}
//Getting Restaurants infos ( name and telephone ) from firebase
holder.mDatabase.child(Constants.STORES_PROFILES + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(holder.map==null){
holder.map = (HashMap<String, String>)dataSnapshot.getValue();
}
if(dataSnapshot.getValue()!=null) {
holder.orderStoreName.setText(holder.map.get(Constants.NAME));
holder.orderTelephone.setText(holder.map.get(Constants.TELEPHONE));
}
//notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//checking Order's status (received, validated, in progress, delivered)
holder.mDatabase.child(Constants.ORDERS + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id() + "/" + holder.order.getOrderId())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
try {
holder.mOrder = dataSnapshot.getValue(Order.class);
if (!holder.order.getStatus().equals(holder.mOrder.getStatus())) {
holder.order.setStatus(holder.mOrder.getStatus());
//update status value for the user
holder.mDatabase.child(Constants.USERS+"/"+ Authentication.getCurrentUser().getUid()+"/"+Constants.SUBMITTED_ORDERS_KEY
+"/"+holder.order.getOrderId()+"/"+Constants.STATUS).setValue(holder.mOrder.getStatus());
//notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
displayOrder(holder);
return vi;
}
public void displayOrder(MyHolder holder){
holder.orderStatus.setText(holder.order.getStatus());
holder.orderNumber.setText(holder.numberText + holder.order.getOrderNumber());
holder.orderDate.setText(Utils.parseDateToDisplay((Long)holder.order.getCreated_at()));
holder.menuCount.setText(holder.order.getMyMealsList().get(0).getQuantity()+"");
holder.orderPrice.setText(holder.order.getTotal_price_formatted());
Glide.with(activity)
.load(holder.order.getMyMealsList().get(0).getMain_image())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.menuPicture);
if(OrdersContainerFragment.pDialog.isShowing()){
OrdersContainerFragment.pDialog.dismiss();
}
}
}
Here is my "xml" file :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/order_history_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="#+id/save_order_linear"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:background="#fcf5ef"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="#+id/order_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="#string/order_number"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:typeface="serif"/>
<TextView
android:id="#+id/order_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="00/00/0000"
android:textColor="#color/colorPrimary"
android:typeface="serif"/>
</LinearLayout>
<LinearLayout
android:id="#+id/save_order_pics"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/save_order_linear"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_weight="74"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:weightSum="100">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33">
<ImageView
android:id="#+id/order_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="#+id/order_progressBar1"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/my_progressbar"
android:padding="22dp"
android:visibility="gone"/>
<TextView
android:id="#+id/order_menu_count1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:gravity="bottom|right"
android:paddingBottom="15dp"
android:paddingRight="15dp"
android:text="ABCD"
android:textColor="#color/gray"
android:textSize="22dp"
android:typeface="serif"/>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="67"
android:orientation="vertical"
android:paddingLeft="15dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="#string/order_price"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_status"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_status"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorAccent"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/Phone"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_phone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_store"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_store"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/save_order_pics"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#fcf5ef"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
I am using a TextView under android.support.v7.widget.CardView. However, whenever I try to set a long text, TextView is not giving the full result.
List<User> userList = new ArrayList<>();
userList.add(new User(R.mipmap.ic_launcher,
"Amit", "9988776655",
"amit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.com"));
This is my java code(as an example) and this shows in the app like this:
for this first one 'Amit'.
How to solve this? If the text is long then it will automatically set it to next line.
Here is my full Cardview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is the snapshot of the java code:Java Snap Shot
this is the java full code
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.UserViewHolder>{
private List<MainActivity.User> userList;
private Context context;
public AllUsersAdapter(List<MainActivity.User> userList, Context context) {
this.userList = userList;
this.context = context;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout, null);
UserViewHolder userViewHolder = new UserViewHolder(view);
return userViewHolder;
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
MainActivity.User user = userList.get(position);
String a=user.getEmailId().toString();
holder.tvProfileName.setText(user.getProfileName());
holder.tvPhoneNumber.setText(user.getPhoneNumber());
holder.tvEmailId.setText(a);
}
#Override
public int getItemCount() {
return userList.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
TextView tvProfileName;
TextView tvPhoneNumber;
TextView tvEmailId;
public UserViewHolder(View itemView) {
super(itemView);
tvProfileName = (TextView) itemView.findViewById(R.id.tvProfileName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
}
Thank you.
use this :
yourTextView.append("\n anotherTextPart")
First of all - if your string will not have white spaces TextView will not able to properly arange text in multi line.
Try to set following attributes :
<TextView
...
android:maxLines="4"/>
Update here is my test that works even without flag above:
MainActivity.java:
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView email;
public ViewHolder(View itemView) {
super(itemView);
email = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_card, parent, false);
return new ViewHolder(card);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.email.setText("test#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.com");
}
#Override
public int getItemCount() {
return 5;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);
view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
view.setAdapter(new Adapter());
}
user_card.xml (changed layout_height to wrap_content):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:src="#drawable/user"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test test test"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
main_activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Result:
Solved by using
TableLayout
and using specific width.full code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="270dip"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="test test test"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="test"
android:layout_below="#id/tvProfileName" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:layout_below="#id/tvPhoneNumber" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>
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
}