Android listview需要很长时间才能加载

时间:2016-07-08 06:35:41

标签: android listview

我有一个自定义适配器的listview,它的工作正常。但是将数据加载到listview需要15秒以上。请告诉我这是什么问题以及为什么花费太多时间?这是我的代码。提前谢谢!

public class Listadapter extends BaseAdapter {
    CheckBox checkBox;
    boolean index[];
    boolean[] itemChecked;
    ApplicationInfo entry;
    String[] itempkg;
    private Context mContext;
    private List<ApplicationInfo> mListAppInfo;
    private PackageManager mPackManager;
    private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

    public Listadapter(Context applicationContext, List<ApplicationInfo> installedApplication, PackageManager packageManager) {
        //super(applicationContext,textViewResourceId,installedApplication);
        super();
        this.mContext = applicationContext;
        this.mListAppInfo = installedApplication;
        index = new boolean[installedApplication.size()];
        this.mPackManager = packageManager;
        for (int i = 0; i < installedApplication.size(); i++) {
            checkList.add(false);
            itemChecked = new boolean[installedApplication.size()];
            itempkg = new String[installedApplication.size()];
        }
    }

    @Override
    public int getCount() {
        return mListAppInfo.size();
        //return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
    }

    @Override
    public Object getItem(int position) {
        // index = new boolean[mListAppInfo.size()];
        return mListAppInfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // get the selected entry

        final ViewHolder holder;

        //  LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            // reference to convertView
            holder.tvAppName = (TextView) convertView
                    .findViewById(R.id.textView1);
            holder.tvPkgName = (TextView) convertView
                    .findViewById(R.id.textView);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.ivAppIcon = (ImageView) convertView
                    .findViewById(R.id.imageView);

            convertView.setTag(holder);
            // holder.ck1.setTag(packageList.get(position));

        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        entry = mListAppInfo.get(position);

        holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
        holder.tvAppName.setText(entry.loadLabel(mPackManager));
        holder.tvPkgName.setText(entry.packageName);
        holder.checkBox.setChecked(false);

        if (itemChecked[position])
            holder.checkBox.setChecked(true);
        else
            holder.checkBox.setChecked(false);

        holder.checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (holder.checkBox.isChecked()) {
                    itemChecked[position] = true;
                    //   SharedPreferences preferences = mContext.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
                    // SharedPreferences.Editor edit = preferences.edit();
                    //  edit.putBoolean("checkbox_" + position, true);
                    //  edit.commit();
                    // editor.putBoolean("CheckBoxState" + position, true);
                } else {
                    itemChecked[position] = false;
                    // editor.putBoolean("CheckBoxState" + position, false);
                    //  SharedPreferences preferences = mContext.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
                    // SharedPreferences.Editor edit = preferences.edit();
                    //  edit.putBoolean("checkbox_" + position, false);
                    // edit.commit();
                }
            }
        });
        return convertView;
    }

    public void setItemChecked3(boolean[] items3) {
        itemChecked = items3;
    }

    public void setItemChecked2(boolean[] items2) {
        itemChecked = items2;
    }

    public void setItemChecked1(boolean[] items1) {
        itemChecked = items1;
    }

    private class ViewHolder {
        ImageView ivAppIcon;
        TextView tvAppName;
        TextView tvPkgName;
        CheckBox checkBox;
    }

}

1 个答案:

答案 0 :(得分:0)

我的解决方案是:使用RecyclerView而不是ListView。

看看下面的示例代码,希望它有所帮助。

public class ViewQuestionActivity extends AppCompatActivity {

    final String TAG = "my_log";
    private RecyclerView recyclerView;
    private RecyclerView.Adapter<MyViewHolder> mAdapter;

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


        mAdapter = new RecyclerView.Adapter<QuestionNameViewHolder>() {
            @Override
            public QuestionNameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(
                        android.R.layout.simple_list_item_1,
                        parent,
                        false);
                QuestionNameViewHolder vh = new QuestionNameViewHolder(v);
                return vh;
            }

            @Override
            public void onBindViewHolder(QuestionNameViewHolder vh, int position) {
                TextView tv = (TextView) vh.itemView;
                tv.setText(questionNameList.get(position));
                tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stars_black, 0, 0, 0);
            }

            @Override
            public int getItemCount() {
                return questionNameList.size();
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

    }

    //-----------------------------------------------------------------------------------------

    // inner class ViewHolder
    private class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public MyViewHolder(View v) {
            super(v);
            v.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            String questionName = ((TextView) v).getText().toString();
            Toast.makeText(getApplicationContext(), getString(R.string.msg_go_details) + questionName, Toast.LENGTH_LONG).show();
            Question question = findQuestionFromName(questionName);
            Intent i = new Intent(ViewQuestionActivity.this, QuestionDetailActivity.class);
            i.putExtra("questionObject", question);
            startActivity(i);
        }
    }

}