简体   繁体   English

如何在循环中一一添加TextView到ListView?

[英]How to add TextView to ListView in the loop one by one?

How to add TextView to LinerLayout in the loop one by one? 如何在循环中一次将TextView添加到LinerLayout? I have a loop of the objects and I want to while running the loop add the TextView to the ListView? 我有一个对象循环,我想在运行循环时将TextView添加到ListView吗?

And I want to add the text in some period of time! 我想在一段时间内添加文字! But with this code it appears after 5 sec and all of them! 但是使用此代码,它会在5秒钟后出现,并且全部出现!

That is what I have tried : 那就是我尝试过的:

itemList=new ArrayList<String>();
        adapter=new ArrayAdapter<String>(this,R.layout.task_text,R.id.txtview,itemList);
        ListView listV=(ListView)findViewById(R.id.list);
        listV.setAdapter(adapter);


for (final Task task : tasks) {


       for (int j = 0; j < 1; j++) {

       itemList.add(task.getName());

       Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
           public void run() {
                    adapter.notifyDataSetChanged();
                    }
             }, 5000);   //5 seconds

My layout Main: 我的布局主:

<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android:id="@+id/line_layout"
    xmlns:tools= "http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".TaskActivity"
    >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

The text_layout: text_layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/txtview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The full code of the Activity: https://repl.it/repls/TransparentIntentRelationaldatabase 活动的完整代码: https : //repl.it/repls/TransparentIntentRelationaldatabase

Use TimerTask to execute it periodically. 使用TimerTask定期执行它。

public class MainActivity1 extends AppCompatActivity  {

  volatile int i = 0; // define as a global variable
  Timer timer; // define as a global variable
  CarerAdapter adapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    ListView listView = findViewById(R.id.listView);
    final ArrayList<String> tasks = new ArrayList<>();
    tasks.add("111");
    tasks.add("222");
    tasks.add("333");
    tasks.add("444");
    tasks.add("555");
    tasks.add("666");
    final ArrayList<String> itemList = new ArrayList<>();
    adapter = new CarerAdapter(this, R.layout.item, itemList);
    listView.setAdapter(adapter);

    int delay = 5000; // delay for 5 sec.
    int period = 5000; // repeat every 5 secs.
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

      public void run() {
        try {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              if (i < tasks.size()) {
                itemList.add(tasks.get(i));
                adapter.notifyDataSetChanged();
                i = i + 1;
              }
            }
          });
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

    }, delay, period);
  }


  @Override
  protected void onStop() {
    if(timer != null) {
      timer.cancel();
      timer = null;
    }
    super.onStop();
  }

  public class CarerAdapter extends ArrayAdapter<String> {

    CarerAdapter(@NonNull Context context, int resource, @NonNull List<String> pCarers) {
      super(context, resource, pCarers);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      // inflate the layout for each list row
      if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).
            inflate(R.layout.text_layout, parent, false);
      }

      String currentItem = getItem(position);
      // get the TextView for item name and item description
      TextView textViewItemName = (TextView)
          convertView.findViewById(R.id.txtview);
      //sets the text for item name and item description from the current item object
      textViewItemName.setText(currentItem);
      // returns the view for the current row
      return convertView;
    }

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个 TextView 中添加各种 editText - How add various editText in one TextView 如何用一个以上的textview填充一个listview中的多个数据? - how to populate multiple data in a listview with more than one textview? 如何在一个ArrayAdapter中将多个textview用于一个listview? - How to use more than one textview within an ArrayAdapter for a listview? 如何在列表视图下方添加文本视图? - How to add a textview below a listview? Android-如何将一个扩展名的文件添加到ListView - Android - How to add files of one extension to listview Android:通过单击按钮,将一个活动的TextView添加到另一个活动的ListView中 - Android: Add TextView from one activty, to ListView in another activity through a button click 如何在Android中的ListView中动态添加textview和按钮? - How to add a textview and a button, dynamically in a ListView in android? 如何在列表视图的列表项之间添加文本视图? - How to add a textview between listitems of listview? 单击custome listview中的按钮时,如何更改特别是一行textview的值? - how to change particularly one row textview value while click the button in custome listview? 如何使用适配器将自定义字体设置为ListView项中的TextView之一? - how to set custom font to one of the TextView's in ListView item using adapters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM