I'm making an alarm app using Android.
Receiving multiple alarms from users was implemented, but the problem was caused by trying to display the set alarms on the screen.
Normally, when we set multiple alarms on our phones, the alarm is added in the same form from the top It's not that the programmer directly adds a TextView to the screen.
It is speculating that a predefined form including TextView, Button, etc. will be saved in a list or something, and then taken out of the list as the user adds an alarm.
Question 1: Is there a way to display the xml file stored in ArrayList or LinkedList on the screen?
Question 2: If so, can you put it in a specific location on the screen?
Question 3: How do I access objects such as textview and button in that xml file?
Thank you for taking the time to read and answer.
android xml arraylist linked-list alarm
You can use the Recycler View.
There used to be something called ListView, which uses a single view several times to display the list.
However, RecyclerView is new to enforce certain patterns in ListView and make them easier to use.
How to use.
Put the code below in the dependency part in the app module cradle.
dependencies {
implementation 'com.android.support:recyclerview-v7:27.1.1'
}
You can put the code below in the xml layout where the list will be displayed.
<android.support.v7.widget.RecyclerView
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>
Please make a ui to repeat Let's create item_information_user_list in layout.
The view below can be recalled several times depending on the amount of data.
<?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_marginTop="10dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/category_text_view_information_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="category"
/>
<TextView
android:id="@+id/content_text_view_information_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="10dp"
Android:text="Where your name, address, and phone number will go."
android:textSize="24sp"
android:textColor="@color/black"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/transparencyDark"
/>
Whether it's fragments or activities, please inflate RecyclerView using findViewById
Next, let's prepare an adapter to use the Recycler View. There are many types and meanings of adapters, but you can understand them as medium legs to use large amounts of data.
// defined as an inner class within the fragment.
private class InformationAdapter extends RecyclerView.Adapter<InformationViewHolder> {
private String[] mItems;
public InformationAdapter(String[] items) {
mItems = items;
}
public void updateData(String[] items) {
mItems = items;
notifyDataSetChanged();
}
@Override
public InformationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_information_user_list, parent, false);
return new InformationViewHolder(v);
}
@Override
public void onBindViewHolder(InformationViewHolder holder, int position) {
String category = "Category";
if (position == 0) category = "address";
else if (position == 1) category = "phone number";
holder.bindViewHolder(category, mItems[position]);
}
@Override
public int getItemCount() {
if (mItems == null) return 0;
return mItems.length;
}
}
Let's create a ViewHolder class to define a single view.
// defined as an inner class within the fragment.
private class InformationViewHolder extends RecyclerView.ViewHolder {
final TextView mCategoryText;
final TextView mContentText;
public InformationViewHolder(View itemView) {
super(itemView);
mCategoryText = itemView.findViewById(R.id.category_text_view_information_list_item);
mContentText = itemView.findViewById(R.id.content_text_view_information_list_item);
}
public void bindViewHolder(String category, String content) {
mCategoryText.setText(category);
mContentText.setText(content);
}
}
View holder classes are 'recycled' by Recyclerview Search for reuse separately!
Now assign the adapter and viewholder to the recycler view.
// Created in the fragment onCreateView().
Data = new String[2]; <- You can write it with alarm-related data. If you change it to the list, please change the adapter accordingly.
mInformationAdapter = new InformationAdapter(data);
mList.setAdapter(mInformationAdapter);
I've written the code so that it can be applied right away. If the code gets longer, you can write the adapter as a separate file, and there are many ways to use it, such as putting decorations between linear layouts, grid layouts, and viewholders. Good bye!
© 2024 OneMinuteCode. All rights reserved.