0

I am trying to put all installed program list into a Spinner. It shows only packageInfo.packageName, but selected item should show packageInfo.sourceDir ,MD5 and SHA1 checksum on TextView below spinner.

As I understand this lines stores information in a dictionary.

    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

And how could retrieve all that information when spinner item selected.

public class DisplayClass extends Activity implements AdapterView.OnItemSelectedListener{
     final PackageManager pm = getPackageManager();
        //get a list of installed apps.
     List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_layout);


        //String tv;
        TextView tv = findViewById(R.id.textView3);
        tv.setMovementMethod( new ScrollingMovementMethod());
        ArrayList<String> progArray = new ArrayList<String>();
        Spinner spinner = findViewById(R.id.spinner);

        for (ApplicationInfo packageInfo : packages) {
            //Log.d(TAG, "Installed package :" + packageInfo.packageName);
            //Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
            String stringName = ("Package : " + packageInfo);
            progArray.add(stringName);
            //Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, progArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public <T extends View> T findViewById(int id) {
        return super.findViewById(id);
    }

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


        Spinner mySpinner=(Spinner) findViewById(R.id.spinner);
        TextView tv = findViewById(R.id.selectedItem);
        String text = mySpinner.getSelectedItem().toString();
        //tv.append("Package : \n" + packageInfo.packageName+'\n');
        //tv.append("Dir : \n" + packageInfo.sourceDir+'\n');
        //File file = new File(packageInfo.sourceDir);
        //String md5 = MD5.calculateMD5(file);
        //String sha1 = SHA1.calculateSHA1(file);
        //tv.setText("MD5 : \n" +md5 +"\n " );
        //tv.append("SHA1 : \n" +sha1 +"\n \n" );

        tv.setText(text);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gerard
  • 13
  • 4
  • What is your error?Can you add log information – Ramesh sambu Dec 15 '17 at 11:57
  • You question is quite broad, maybe you should ask how to obtain the APK file and calculate the MD5 hash of it? – Rolf ツ Dec 15 '17 at 12:56
  • Possible duplicate of [How can i get the apk file name and path programmatically?](https://stackoverflow.com/questions/16973248/how-can-i-get-the-apk-file-name-and-path-programmatically) – Rolf ツ Dec 15 '17 at 12:57
  • there's no error, All I want to do is when selecting some package it would show me that package directory. Exm. if select com.package.name in spinner it would show in TextView that package directory. Problem I don't know how to do it when all I have is String from **packageInfo** – Gerard Dec 15 '17 at 12:58
  • I will try explain in different way. When I run for cycle i put all packages in spinner. When clicked it return just a String. with that String or something I should be able to reach primary list which is **List packages** or **ApplicationInfo packageInfo : packages** to get that package all info which also contains directory, name and so on. – Gerard Dec 15 '17 at 13:02

0 Answers0