简体   繁体   中英

How to add TextView to ListView in the loop one by one?

How to add TextView to LinerLayout in the loop one by one? I have a loop of the objects and I want to while running the loop add the TextView to the 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!

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:

<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

Use TimerTask to execute it periodically.

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

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM