简体   繁体   中英

How to set text to value of listview items after time intervals

I am developing an android app where a user will input text to an EditText , then it will be added to a ListView.

But once a button with onClick value "start" is clicked, it will display each item of the ListView in a TextView with id displayText, in the order of each item and it will loop(Repeat) once it is exhausted.

Here is my Code

Math.java

public class Math extends AppCompatActivity {

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

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

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

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

Okay, i think the issue is that you haven't given the TextView any thing to display. you have to convert the arrayList to a String array and use setText to display the Value. add this part to the "start" method

String ls =System.getProperty("line.separator");// i used this to seperate the array items 
           String joint = TextUtils.join(ls, lvitems);
            result.setText(joint);

That should work

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