简体   繁体   中英

List view data is overriding in 1st list item always

I am developing a simple listview with a Image, text and Image in every list item.

But always the data is overriding in 1st list item only.

here is my code.

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;

        }

    };
}

//Screen shot is as below. Please help

在此输入图像描述

change this:

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

to

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

applies to all the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM