Java 如何以编程方式增加Android中的文本数量

Java 如何以编程方式增加Android中的文本数量,java,android,Java,Android,我有一个从数据库中获取开始时间和结束时间的页面 假设开始时间是7:00,结束时间是22:00 我想使用此开始时间和结束时间在我的页面中显示为文本视图,如7:00 8:00 9:00,然后一直显示到22:00作为文本视图 我还有一个图像视图,当文本增加时,它也会增加 我怎样才能做到这一点 另外,我希望在水平滚动视图中显示结果文本,在每个Imageview的顶部显示Imageview,在底部显示文本视图 char first = StartTime.charAt(0); int StartTime

我有一个从数据库中获取开始时间和结束时间的页面

假设开始时间是7:00,结束时间是22:00

我想使用此开始时间和结束时间在我的页面中显示为文本视图,如7:00 8:00 9:00,然后一直显示到22:00作为文本视图

我还有一个图像视图,当文本增加时,它也会增加

我怎样才能做到这一点

另外,我希望在水平滚动视图中显示结果文本,在每个Imageview的顶部显示Imageview,在底部显示文本视图

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));
char first=StartTime.charAt(0);
int StartTimeint=Integer.parseInt(String.valueOf(first));
int l;

对于(l=StartTimeint;l您在layout.XML文件中编写的用于创建UI的XML代码仅用于静态UI。您要求的是在运行时动态创建视图。尽管您可以通过单击按钮或其他方式使用java代码创建视图。但最好尽可能少为UI编写代码,并保持其可用性从程序代码中分离出来。使用我们正在使用的框架提供给我们的工具

在Android中,这些工具包括
ListView
GridView
和更新更好的
RecyclerView
。这些视图可以帮助您在运行时将其他视图动态添加到UI中。您可以定义其中一个或多个视图(取决于您的UI需要)一旦进入layout.xml,就可以像其他视图一样使用java代码对其进行配置

这就是如何使用RecyclerView实现目标的方法。我无法解释RecyclerView的工作原理以及每行代码的作用,因为这将是一篇很长的文章,但我已尝试简要介绍主要内容

1。在布局文件中添加RecyclerView

活动\u 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>
<?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>
4.创建一个
适配器
类,该类从
RecyclerView.Adapter
扩展而来。适配器充当UI数据和RecyclerView本身之间的桥梁。适配器告诉RecyclerView要充气的布局文件和充气的数量。RecyclerView的任务是处理如何在UI上充气

EDIT:刚刚更改了
onCreateViewHolder()
中的
myViewHolder
,以匹配修改后的
myViewHolder
构造函数。在
onBindViewHolder()中添加了对
setItemPosition()
的调用

MyAdapter.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();

    }
} 
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());
    }
}
这是最终结果


如下更改活动布局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">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        ...>

        <LinearLayout
            android:id="@+id/container"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

将textview和imageview移动到另一个XML文件,我们称之为item_view.XML(您可以随意命名)。之所以这样做,是因为此文件的根视图将被重用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="150px"
        android:layout_height="150px"
        app:srcCompat="@drawable/seat"/>


    <TextView
        android:id="@+id/timeofseats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="7:00"
        android:textColor="#fff"
        android:textSize="20dp"/>

</LinearLayout>

现在在Java文件中进行以下更改

    LinearLayout container = findViewById(R.id.container); // or rootView.findViewById() for custom View and Fragment

    char first = StartTime.charAt(0);

    int StartTimeint = Integer.parseInt(String.valueOf(first));

    for(int l = StartTimeint; l<=22; l++){
        Log.d("SeatsPage", "Time is "+l);
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_view, null);
        TextView timeofseats = view.findViewById(R.id.timeofseats);
        timeofseats.setText(Integer.toString(l));
        container.addView(view);
    }
LinearLayout container=findviewbyd(R.id.container);//或用于自定义视图和片段的rootView.findviewbyd()
char first=StartTime.charAt(0);
int StartTimeint=Integer.parseInt(String.valueOf(first));

对于(intl=StartTimeint;lSmall hint)这里的set
timeofssets.setText(Integer.toString(l))
应该在
for
循环中。你得到23,因为这是你当前逻辑的工作方式。循环结束时
l
将具有
23
的值。这就是你编写逻辑的方式,你不这么认为吗?当
l
达到
23
时打破循环。但这不是主要问题。我想知道您想要多个
TextView
来显示时间间隔,还是只想要一个
TextView
来显示
7:00.8:00.9:00…22:00
作为单个字符串?是的,我想要多个TextView来显示时间间隔time@SyedAhmedJamil我如何做到这一点我认为你的问题与用户界面逻辑有关,而不是程序逻辑gic.你能更新你的问题并包括你的布局xml文件的内容吗?还可以附上一个运行应用程序的屏幕截图,以更好地了解你的UI。谢谢你的回答!但是如何在每个项目上获得onClick Listener???@Syed Ahmed Jamil。有很多方法可以使用。如果你搜索堆栈溢出,你会发现像and或你的c这样的技术按照我更新了答案的文档进行艰难的操作。参见第3步和第4步。只需根据第3步和第4步中的更新更新您的代码。我这样做的方式可能不是最好的方式。我所做的主要是在
MyViewHolder
类上实现
View.OnClickListener
。这样您就可以设置当RecyclerView在
OnCreateViewHolder()
中为一个项目创建一个新的视图支架时,每个项目的视图上都有一个onClickListener。单击每个项目时,您想做什么,请在
MyViewHolder
类的
onClick()
方法中执行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="150px"
        android:layout_height="150px"
        app:srcCompat="@drawable/seat"/>


    <TextView
        android:id="@+id/timeofseats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="7:00"
        android:textColor="#fff"
        android:textSize="20dp"/>

</LinearLayout>
    LinearLayout container = findViewById(R.id.container); // or rootView.findViewById() for custom View and Fragment

    char first = StartTime.charAt(0);

    int StartTimeint = Integer.parseInt(String.valueOf(first));

    for(int l = StartTimeint; l<=22; l++){
        Log.d("SeatsPage", "Time is "+l);
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_view, null);
        TextView timeofseats = view.findViewById(R.id.timeofseats);
        timeofseats.setText(Integer.toString(l));
        container.addView(view);
    }