首页 > 解决方案 > 如何在循环中将TextView一一添加到ListView?

问题描述

如何在循环中将TextView一一添加到LinerLayout?我有一个对象循环,我想在运行循环时将 TextView 添加到 ListView?

我想在一段时间内添加文字!但是使用此代码,它会在 5 秒后出现,并且全部出现!

这就是我尝试过的:

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

我的布局主要:

<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>

文本布局:

<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>

Activity 完整代码: https ://repl.it/repls/TransparentIntentRelationaldatabase

标签: javaandroid

解决方案


用于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;
    }

  }

}

推荐阅读