安卓 如何限制列表视图项目显示?



现在我正在使用本地数据库在应用程序上获取和显示项目,但我想拆分项目并将它们显示在不同的页面 1、2、3 等中。我该怎么做?这是以列表视图格式显示数据和显示所有项的代码。如何限制列表中的项目数?如果你知道的话,也请告诉我在哪里进行更改。谢谢

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
listView = (ListView)findViewById(R.id.listView);
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
new Connection().execute();
btn =(Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity2();
}
});
}
private void openActivity2() {
Intent in = new Intent(this,Main2Activity.class);
startActivity(in);
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
class Connection extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
String result = "";
String host = "http://10.0.3.2/Client/docks.php";
try
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(host));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
while ((line = reader.readLine())!=null)
{
stringBuffer.append(line);
break;
}
reader.close();
result = stringBuffer.toString();

}
catch(Exception e)
{
return new String("There exception: "+e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
try {
JSONObject jsonResult = new JSONObject(result);
int success = jsonResult.getInt("success");
if(success==1)
{
JSONArray data = jsonResult.getJSONArray("abc");
for(int i = 0; i < data.length(); i++)
{
JSONObject data1 = data.getJSONObject(i);
int id = data1.getInt("dock_id");
String name = data1.getString("dock_name");
String description = data1.getString("dock_desc");
int flightarrival = data1.getInt("flight_arrival");
int count = data1.getInt("trolley_count");
String time = data1.getString("snapshot_time");
String line = id+"."+name+"-"+count+"-"+flightarrival+"-"+time;
adapter.add(line);



}
}
else
{
Toast.makeText(getApplicationContext(), "there is no data", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if( id == R.id.home)
{
Toast.makeText(this,"This is home", Toast.LENGTH_SHORT).show();
}
if( id == R.id.trolleycount)
{
Toast.makeText(this,"This is trolleycount", Toast.LENGTH_SHORT).show();
}
if( id == R.id.log)
{
Intent in = new Intent(this,MainActivity.class);
startActivity(in);
Toast.makeText(this,"You have successfully logged out of your account", Toast.LENGTH_SHORT).show();
}
return false;
}

看起来您可以在onPostExecute()方法中修改此代码:

JSONArray data = jsonResult.getJSONArray("abc");
for(int i = 0; i < data.length(); i++)
{
...
adapter.add(line);
}

您可以做的是更改循环条件,以检查i是否小于data.length()以及您想要施加的任何限制。假设您的限制为 100:

JSONArray data = jsonResult.getJSONArray("abc");
for(int i = 0; i < Math.min(100, data.length()); i++)
{
...
adapter.add(line);
}

Math.min()方法将返回两个数字中较小的一个,因此当项目少于 100 个时,您将获得data.length()项,当有更多项时,您将获得 100 项。

最新更新