android如何限制ListView显示的项目?

时间:2018-10-03 02:17:56

标签: android listview arraylist

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

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

1 个答案:

答案 0 :(得分:0)

您似乎可以在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);
}

The Math.min() method将返回两个数字中较小的那个,因此当少于100个项目时,您将获得data.length()个项目;而当数目更多时,您将得到100个项目。