列表视图数据始终覆盖在第1个列表项中

时间:2013-10-15 12:21:00

标签: android

我正在开发一个简单的列表视图,其中包含每个列表项中的图像,文本和图像。

但是数据总是只覆盖第一个列表项。

这是我的代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview = (ListView) findViewById(R.id.apps_list);
    am = (ActivityManager) this.getSystemService(this.ACTIVITY_SERVICE);
    lv = am.getRunningTasks(Integer.MAX_VALUE);
// Actually this should be in timer
new RetriveStock().execute();
}

// AsyncTask

public class RetriveStock extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        updateList();
        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                listview.invalidateViews();
                listview.setAdapter(adb);
                adb.notifyDataSetChanged();
                listview.requestLayout();
            }
        });

        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}

public void updateList() {
    adb = new ArrayAdapter<RunningTaskInfo>(MainActivity.this,
            R.layout.app_list, lv) {

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View view = convertView;
            try {
                if (null == view) {
                    LayoutInflater vi = (LayoutInflater) MainActivity.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.app_list, null);
                }
                TextView appName = (TextView) findViewById(R.id.appName);
                ImageView icon = (ImageView) findViewById(R.id.icon);
                ImageButton close = (ImageButton) findViewById(R.id.close);
                RunningTaskInfo app = lv.get(position);
                ApplicationInfo ai = new ApplicationInfo();
                PackageManager pm = getApplicationContext()
                        .getPackageManager();
                try {
                    ai = pm.getApplicationInfo(
                            app.baseActivity.getPackageName(), 0);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
                final String applicationName = (String) (ai != null ? pm
                        .getApplicationLabel(ai) : "(unknown)");
                appName.setText(applicationName);
                icon.setImageDrawable(pm.getApplicationIcon(ai));


            } catch (Exception e) {
                e.printStackTrace();
            }
            return view;

        }

    };
}

//屏幕截图如下。请帮忙

enter image description here

1 个答案:

答案 0 :(得分:2)

改变这个:

TextView appName = (TextView) findViewById(R.id.appName); 

TextView appName = (TextView)view. findViewById(R.id.appName); 

适用于所有其他人。