我有一个页面,我在其中从数据库中获取开始时间和结束时间。
假设开始时间是 7:00,结束时间是 22:00
我想使用这个 START TIME 和 END TIME 在我的页面中显示为 textview,如 7:00 8:00 9:00 和 sooo 直到 22:00 作为 textview
我还有一个 ImageView ,当文本增加时也会增加。
我怎样才能做到这一点?
我也想要水平 ScrollView 中的结果文本,顶部的 Imageview 和每个 imageview 的底部的 TextView
char first = StartTime.charAt(0);
int StartTimeint = Integer.parseInt(String.valueOf(first));
int l;
for( l = StartTimeint; l<=22; l++){
Log.d("SeatsPage", "Time is "+l);
}
timeofseats.setText(Integer.toString(l));
这是我到目前为止所做的,但结果是我得到了 23,textview 没有增加
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/llMain"
android:layout_height="match_parent"
tools:context=".SeatsPagewithDB.SeatsPage">
<ImageView
android:id="@+id/imageView11"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginStart="28dp"
android:layout_marginEnd="326dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/seat" />
<TextView
android:id="@+id/timeofseats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="334dp"
android:background="#FF0000"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="7:00"
android:textColor="#fff"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView11" />
</android.support.constraint.ConstraintLayout>
This is the result I am getting as layout
This what I want programmatically
最佳答案
您在 layout.xml 文件中编写的用于创建 UI 的 XML 代码仅用于静态 UI。您要问的是在运行时动态创建 View 。尽管您绝对可以通过单击按钮或其他方式使用 java 代码创建 View 。但最好尽可能少为 UI 编写代码,并将其与程序代码分开。而是使用我们正在使用的框架提供给我们的工具。
在 Android 中,这些工具包括像 ListView
这样的东西。 , GridView
和更新更好的RecyclerView
.这些 View 可帮助您在运行时将其他 View 动态添加到 UI。您在 layout.xml 中定义一个或多个(取决于您的 UI 需求)一次,并像任何其他 View 一样使用 java 代码配置它们。
这就是你如何使用 RecyclerView 来实现你的目标。我无法解释 RecyclerView 的所有工作原理以及每行代码的作用,因为它会写一个很长的帖子,但我试图简要地强调主要内容。
1. 在布局文件中添加 RecyclerView。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 创建另一个布局文件并定义 RecyclerView 将要显示的项目的模板 UI。 RecyclerView 将使用此布局填充它保存的每个项目。
item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView_alarm"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/alarm" />
<TextView
android:id="@+id/textView_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#FF0000"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Time"
android:textColor="@android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
3. 创建
ViewHolder
从 RecyclerView.ViewHolder
扩展的类. View holder 是一个 RecyclerView 相关的概念。简而言之,它作为单个项目 View 的包装器,有助于将新数据绑定(bind)到项目 View 。创建 bind()
内置 View 支架,让您的生活更轻松。编辑:我通过实现
View.OnClickListener
更新了类(class)接口(interface),修改构造函数传入context
来自 onCreateViewHolder()
并添加 setItemPosition()
只是为了从 onBindViewHolder()
传递项目位置编号全部到这里,这样我们就可以在我们的 onClick()
中使用这个职位编号接口(interface)方法MyViewHolder.java [更新]
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
private int itemPosition;
private Context mContext;
public MyViewHolder(@NonNull View itemView, Context context) {
super(itemView);
itemView.setOnClickListener(this);
mContext = context;
textView = itemView.findViewById(R.id.textView_Time);
}
void bind(String timeText)
{
textView.setText(timeText);
}
void setItemPosition(int position)
{
itemPosition = position;
}
@Override
public void onClick(View v) {
Toast.makeText(mContext, "You clicked item number: " + itemPosition , Toast.LENGTH_SHORT).show();
}
}
4. 创建
Adapter
从 RecyclerView.Adapter
扩展的类. Adapter 作为 UI 数据和 RecyclerView 本身之间的桥梁。一个适配器告诉 RecyclerView 什么布局文件要膨胀以及要膨胀多少。 RecyclerView 的工作是处理如何在 UI 上充气。编辑:刚改
myViewHolder
在 onCreateViewHolder()
匹配 MyViewHolder
的修改后的构造函数.添加了对 setItemPosition()
的调用在 onBindViewHolder()
.MyAdapter.java [更新]
public class MyAdapter extends RecyclerView.Adapter {
List<String> timeIntervalList = new ArrayList<>();
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view , parent.getContext());
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.setItemPosition(position);
viewHolder.bind(timeIntervalList.get(position));
}
@Override
public int getItemCount() {
return timeIntervalList.size();
}
public void addItem (String timeText)
{
timeIntervalList.add(timeText);
notifyItemInserted(getItemCount());
}
}
在此适配器中,您将看到两个功能。
OnCreateViewHolder()
使用单个项目的模板布局文件和 OnBindViewHolder()
扩展 View 将新数据绑定(bind)到刚刚创建的 View 的默认值。用于绑定(bind)的数据存储在此适配器内的列表中,称为 timeIntervalList
。 .此列表将保存您的时间间隔字符串,以便它们可以在 View 上更新。5. 最后,在你想使用它的地方使用这个 RecyclerView。就像在您的 MainActivity.java 中一样。需要使用
LayoutManager
告诉 RecyclerView 以何种方式显示项目(例如列表、网格等) . LinearLayoutManager
将垂直或水平显示项目。您可以看到我正在使用您的逻辑从字符串中增加时间并使用 addItem()
将新 View 添加到 RecyclerView MyAdapter
的功能类(class)。MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView myRecyclerView;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = findViewById(R.id.recyclerView);
myAdapter = new MyAdapter();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this , LinearLayoutManager.HORIZONTAL, false);
myRecyclerView.setLayoutManager(linearLayoutManager);
myRecyclerView.setAdapter(myAdapter);
// This is how you will populate the recycler view
String START_TIME = "7:00";
String END_TIME = "22:00";
char first = START_TIME.charAt(0);
int StartTimeint = Integer.parseInt(String.valueOf(first));
int l;
for( l = StartTimeint; l<=22; l++){
// This is where new item are added to recyclerView.
myAdapter.addItem(l + ":00");
}
}
}
这是最终的结果。
关于java - 如何以编程方式增加Android中的文本数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860570/