gpt4 book ai didi

java - 如何在 ListView 到达末尾时向其添加项目

转载 作者:行者123 更新时间:2023-12-01 12:53:35 25 4
gpt4 key购买 nike

现在我有一个 ListView,其中充满了我的“位置”对象。我的 list 中最初有 10 项。当我读完时,我想再添加 10 个项目。

代码:

public class MainActivity extends ActionBarActivity {

ListView listView;
int startLoop, endLoop;
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
Button refresh;
private ProgressDialog progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
startLoop = 0;
endLoop = 10;
listView = (ListView) findViewById(R.id.listView1);
progress = new ProgressDialog(this);
listView.setOnScrollListener((OnScrollListener) this);

// Construct the data source
arrayOfLocations = new ArrayList<Location>();

// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);

FillLocations myFill = new FillLocations();
myFill.execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private class FillLocations extends AsyncTask<Integer, Void, ArrayList<Location>> {

protected void onPreExecute() {

}

@Override
protected ArrayList<Location> doInBackground(Integer... params) {

for (int i = startLoop; i < endLoop; i++) {

String title = "Place " + Integer.toString(i);
locArray.add(new Location(title, "details","hours","distance:)));

}

return locArray;
}

protected void onPostExecute(ArrayList<Location> listOfLocs) {
// Attach the adapter to a ListView

for (Location location : listOfLocs)
adapter.add(location);

listView.setAdapter(adapter);

progress.dismiss();

}
}

}

所以基本上我想要这样的东西: 1.检测何时到达终点 2.loopStart和loopEnd加10 3.调用我的 FillLocations 任务

关于如何实现这一点有什么想法吗?谢谢

最佳答案

你要么想要:

  1. 填充新项目时加载文本/动画:How to display a "Loading..." text while retrieving items for a ListView

  2. 一个简单的无尽列表,没有花哨的东西:endless scroll list view in android

  3. 用户通过拉动刷新机制触发填充:How to implement Android Pull-to-Refresh

关于java - 如何在 ListView 到达末尾时向其添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24042875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com