每秒从列表视图中显示字符串 [英] Display Strings from a listView every second

查看:190
本文介绍了每秒从列表视图中显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,其中用户将文本输入到EditText,单击一个按钮后,它将被添加到ListView。

I am developing an android app where the user inputs text to an EditText and once a button is clicked, it will be added to a ListView.

public void AddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    itemsAdapter.add(itemText);
    etNewItem.setText("");
}

我希望listview中每个项目的值显示在textView中,1个项目接一个,间隔1秒。

I want the value of each item in the listview to be shown in a textView, one Item after another on an interval of 1 second.

这是我的完整主要活动代码:

Here is my full main activity code:

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

public void AddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    itemsAdapter.add(itemText);
    etNewItem.setText("");
}
public void start(View v) throws JSONException {
    RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
    lc.setVisibility(View.GONE);
    RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
    tc.setVisibility(View.VISIBLE);


    }
}

activity_math.xml代码如下。

activity_math.xml code is as follows.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Math">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tvContainer"
    android:visibility="gone">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:id="@+id/displayText"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/contentContainer">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lvItems"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/btnAddItem" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etNewItem"
        android:layout_alignTop="@+id/btnAddItem"
        android:hint="Enter a new item"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/btnAddItem"
        android:layout_toStartOf="@+id/btnAddItem"
        android:layout_alignParentBottom="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:id="@+id/btnAddItem"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="AddItem"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/btnAddItem"
        android:onClick="start"/>

</RelativeLayout>
</RelativeLayout>

总之,我想知道如何每秒使用ListView的每个值来更新TextView。 / p>

In total, I am wondering how I can update TextView using each value of the ListView each second.

推荐答案

试试这个编辑过的答案。将其添加到您的按钮。

try this edited answer. Add this to your button.

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

我认为你有错误。

public void AddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    items.add(itemText); // change the adapter to your ArrayList
    etNewItem.setText("");
    itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    lvItems.setAdapter(itemsAdapter);
}

您的计时器将此添加到您的onCreate

for your Timer add this to your onCreate

new Timer().scheduleAtFixedRate(new TimerTask() {
  @Override
  public void run() {
    if (items.size() > 0) {
      for (int i = 0; i < items.size(); i++) {
        displayText.setText(displayText.getText().toString() + "\n" + items.get(i)); 
      }
    } else {}
  }
},0,1000);

这篇关于每秒从列表视图中显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆