自定义列表视图加载应用程序需要很长时间

时间:2016-10-20 12:19:20

标签: java android performance

我从api获取json数据,我想在我的自定义列表视图中显示两个字段,即项目编号和简短描述。一切正常,数据c =也出现了,但代码用数据填充listview需要很长时间。任何人都可以提出任何建议,或者我做错了什么?下面是我的代码: -

public class AllTickets_del extends ActionBarActivity {
private android.support.v7.widget.Toolbar toolbar;

android.widget.ListView list;
ArrayList<String> web=new ArrayList<>();
ArrayList<String> tktnumber=new ArrayList<>();
ArrayList<String> status=new ArrayList<>();
String myurl="";

public class DownloadTask extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... params) {
        String result="";
        URL url;

        HttpURLConnection urlConnection;


        try{

            url= new URL(params[0]);
            URI uri = new URI(url.getProtocol(), url.getUserInfo(),
                    url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
            url = uri.toURL();
            urlConnection=(HttpURLConnection)url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data =  reader.read();
            while(data!=-1){
                char current = (char)data;
                result+=current;
                data=reader.read();
            }

            try {
                JSONObject jsonobject = new JSONObject(result);
                String info= jsonobject.getString("result");
                JSONArray arr = new JSONArray(info);
                for (int i=0;i<arr.length();i++){
                    JSONObject jsonpart= arr.getJSONObject(i);
                    web.add(jsonpart.getString("short_description"));
                    tktnumber.add(jsonpart.getString("number"));
                    status.add(jsonpart.getString("incident_state"));

                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("json", "catch");
            }

            final Intent tktdetails= new Intent(getApplicationContext(), Ticketdetails.class);



            CustomList_del adapter = new
                    CustomList_del(AllTickets_del.this, web,tktnumber);
            list=(ListView)findViewById(R.id.list);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    String ticketnumber= tktnumber.get(+position);
                    String ticketcontent= web.get(+position);

                    tktdetails.putExtra("ticketnumber", ticketnumber);
                    tktdetails.putExtra("ticketcontent", ticketcontent);
                    startActivity(tktdetails);

                }
            });



        }catch(Exception e){
            e.printStackTrace();
            Log.i("bigger","catch");
        }
        return result;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_tickets_del);



    toolbar=(android.support.v7.widget.Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    myurl="URL from where im getting data";
    String result="";
    DownloadTask task= new DownloadTask();
    try {
        task.execute(myurl).get();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_all_tickets_del, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题。我使用了android给我们的可能性。

我想在聊天中延迟加载消息,因为可能有很多消息。

我有一个自定义适配器,我提供了所有ID的列表。只有在滚动列表视图时才会调用适配器getView,并且很快就会显示该项目。所以在这部分我实际加载了消息。

我不知道你的服务器是否提供了这样的东西,或者你是否可以抓住

JSONObject jsonpart= arr.getJSONObject(i);
web.add(jsonpart.getString("short_description"));
tktnumber.add(jsonpart.getString("number"));
status.add(jsonpart.getString("incident_state"));

并将其放入适配器的getView方法。