如何使用listView每秒更新TextView

时间:2017-12-06 16:52:24

标签: java android listview

我有一个简单的Android应用程序。在该应用程序内部,有一个editText,它在按钮点击时将项目输入到listView。然后,当按下onClick' 开始''单击它,它会显示一个textView,它显示整个listview项目。

public class Math extends AppCompatActivity {

private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
TextView displayText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_math);

    lvItems = (ListView) findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    itemsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    lvItems.setAdapter(itemsAdapter);
    displayText = (TextView)findViewById(R.id.displayText);
    //displayText is textView that displays list values.
}

public void AddItem(View v) {
    //Where items are added to the list
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    items.add(itemText);
    etNewItem.setText("");
}
public void start(View v) {
    //Where it displays text
    RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
    lc.setVisibility(View.GONE);
    RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
    tc.setVisibility(View.VISIBLE);

    for (int i = 0; i < items.size(); i++) {
        displayText.setText(displayText.getText().toString() + "\n" + items.get(i));
    }


}
}

有没有办法可以每秒一个一个地在displayText中显示listView项目。

1 个答案:

答案 0 :(得分:1)

根据您的要求::

,这是更新的代码
  public class Math extends AppCompatActivity {

    int listSize=0;
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;
    TextView displayText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_math);

       lvItems = (ListView) findViewById(R.id.lvItems);
       items = new ArrayList<String>();
       itemsAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, items);
       lvItems.setAdapter(itemsAdapter);
       displayText = (TextView)findViewById(R.id.displayText);
      //displayText is textView that displays list values.
 }

 public void AddItem(View v) {
    //Where items are added to the list
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    items.add(itemText);
    etNewItem.setText("");
 }
 public void start(View v) {
     //Where it displays text
     RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
     lc.setVisibility(View.GONE);
     RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
     tc.setVisibility(View.VISIBLE);

     final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if(count>items.size())
                        timer.cancel();
                    else {
                        displayText.setText(displayText.getText().toString() + "\n" + items.get(count));
                    }
                }
            });

        }
    }, 0, 1000);

     }


 }
 }

我认为它会按照你的期望工作,如果有什么要改变请告诉我,我会做。

感谢和快乐的编码。