I am getting a ClassCastException for the pid variable that I am trying to show on screen. I know that the item at the onclick position is a string that is casting to RunningAppProcessInfo which is causing the exception, but I cant figure out how I can get the pid without using it though.
Here is my code
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();
ArrayList<String> info = new ArrayList<>();
for (ActivityManager.RunningAppProcessInfo p : list) {
info.add(p.processName);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, info);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int uid = ((RunningAppProcessInfo)parent.getItemAtPosition(position)).pid;
Toast.makeText(getApplicationContext(),uid,Toast.LENGTH_LONG).show();
}
});