如何在循环中一一添加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>
活动的完整代码: https://repl.it/repls/TransparentIntentRelationaldatabase
答案 0 :(得分:1)
使用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;
}
}
}