2
final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
final PackageManager pm = getApplicationContext().getPackageManager();
ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);

//Drawable iconApp = resolveInfo.activityInfo.loadIcon(getPackageManager());

ApplicationAdapter adapter = new ApplicationAdapter(this, listP);

adapter.addListener(this);
ListView list = (ListView)findViewById(R.id.list);

list.setAdapter(adapter);

このコードは利用可能なすべてのアプリケーションを表示しますが、結果に取り組みたいと思います。あなたが持っているすべての行com.android.*で、それは私がカットしたい部分です.

問題は、substring(10)たとえば使用しようとしたときに結果が変わらないことです。

私はそのように試してみましたが、Logで部分文字列が成功しましたが、画面に表示すると、すべてのボタンを含む別のレイアウトが表示されます。このコードでほとんど取得できます。画面に表示したいものをLogで表示しますが、ログで正しい結果が得られましたが、理由がわかりません

public void onCreatebis() {

        setContentView(R.layout.main);
        final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
        final PackageManager pm = getApplicationContext().getPackageManager();
        ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);
        final int trimLength = "com.android.".length();
        ArrayList<String> maliste = new ArrayList();
        int size=listP.size();
        size=maliste.size();
       //String []maliste=new String[listP.size()];
        // Loop over each item.
        for (ResolveInfo info : listP) {
            // Get the (full, qualified) package name.
            String packag = info.activityInfo.applicationInfo.packageName;

            // Now, trim it with substring and the trim length.
            String trimmed = packag.substring(trimLength);
            for(int i=0;i<maliste.size();i++){
                maliste.set(i, trimmed);
            }
            Log.v("trimmed", trimmed);

            // [ do whatever you want with the trimmed name ]
        }




        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste);

        ListView list = (ListView)findViewById(R.id.list);

        list.setAdapter(adapter2);
4

2 に答える 2

2

を使用するのは正しい考えsubstringです。これが私がそれを行う方法です:

// Get the length of text to trim.
final int trimLength = "com.android.".length();

// Loop over each item.
for (ResolveInfo info : listP) {
    // Get the (full, qualified) package name.
    String package = info.activityName.packageName;

    // Now, trim it with substring and the trim length.
    String trimmed = package.substring(trimLength);

    // [ do whatever you want with the trimmed name ]
}

の値はtrimLength12 と評価されるため、どうやって 10 を取得したのかわかりませんが、これは機能するはずです。

于 2013-03-11T16:16:09.360 に答える
0

あなたの助けを借りてWCharginに感謝します私はこれを行いました小さな違いがありますが、とにかくコードがうまく動作するという深い違いはわかりません

public void onCreatebis() {

        setContentView(R.layout.main);
        final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
        final PackageManager pm = getApplicationContext().getPackageManager();
        ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities( mainIntent, 0);
        final int trimLength = "com.android.".length();
        ArrayList<String> maliste = new ArrayList<String>();


       //String []maliste=new String[listP.size()];
        // Loop over each item.
        for (ResolveInfo info : listP) {
            // Get the (full, qualified) package name.
            String packag = info.activityInfo.applicationInfo.packageName;

            // Now, trim it with substring and the trim length.
            String trimmed = packag.substring(trimLength);
            maliste.add(trimmed);



        }

        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste);

        ListView list = (ListView)findViewById(R.id.list);

        list.setAdapter(adapter2);



    }

そのソリューションの問題は、最後に文字列があることです。もちろん、すべての文字列の配列を作成してアダプターに渡すことができますが、使用していたため、もう実行できなかった他の操作がありましたResolveInfo の配列リストであり、文字列の配列リストではありません 。独自のリスナーをリストに追加します https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener

于 2013-03-12T11:04:52.070 に答える