2016-01-13 92 views
-1

如何從每個列表視圖項目的左側添加可繪製文件夾中的縮略圖圖像?在列表視圖項目旁邊添加縮略圖

我也想算的列表項目的數目,在我的TextView顯示它們

任何幫助,將不勝感激! IM停留在這個很長一段時間

public class MainActivity extends Activity { 

    ArrayList<String> results = new ArrayList<String>(); 
    ArrayList<String> packageNames = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getInstalledApps(this); 
     ArrayAdapter adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_listview, results); 

     ListView listView = (ListView) findViewById(R.id.mobile_list); 
     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       String packageName = packageNames.get(position); 
       Intent intent = new Intent(
         android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       intent.setData(Uri.parse("package:" + packageName)); 
       startActivity(intent); 
      } 
     }); 
    } 

    private ArrayList<String> getInstalledApps(Context context) { 
     PackageManager packageManager = context.getPackageManager(); 
     List<PackageInfo> applist = packageManager.getInstalledPackages(0); 
     Iterator<PackageInfo> it = applist.iterator(); 
     while (it.hasNext()) { 
      PackageInfo pk = (PackageInfo) it.next(); 
      if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 
       Log.v("system app using internet = ", 
         "" + pk.applicationInfo.loadLabel(packageManager)); 
       continue; 
      } 

      if (PackageManager.PERMISSION_GRANTED == packageManager 
        .checkPermission(Manifest.permission.INTERNET, 
          pk.packageName) 
        || PackageManager.PERMISSION_GRANTED == packageManager 
          .checkPermission(
            Manifest.permission.READ_EXTERNAL_STORAGE, 
            pk.packageName)) { 
       results.add("" + pk.applicationInfo.loadLabel(packageManager)); 
       packageNames.add(pk.packageName); 
      } 
     } 

     Log.v("app using internet = ", results.toString()); 

     return results; 
    } 
} 
+0

更改適配器的'getView'方法。 – Rohit5k2

+0

嗨,你可以參考這個鏈接。 http://www.vogella.com/tutorials/AndroidListView/article.html – Nik

回答

0

試試這個:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results) { 
    @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      TextView textView = (TextView) super.getView(position, convertView, parent); 
      //Change this to your drawable 
      Drawable myDrawable = getResources().getDrawable(android.R.drawable.ic_dialog_alert) 
      textView.setCompoundDrawables(myDrawable , null, null, null); 
      return textView; 
     } 
}; 
+0

我應該在哪裏插入?或將其替換爲?謝謝 – user5784363

+0

用這個代碼代替'ArrayAdapter adapter = new ArrayAdapter (this, R.layout.activity_listview,results)'。 –

+0

我做了,它不做任何事,我沒有看到任何圖像。我需要在佈局中使用imageview嗎? – user5784363

0

如果您正在使用然後自定義適配器做到這一點在你的getView方法

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.row_layout, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    textView.setText(values[position]); 

    String s = values[position]; 
    imageView.setImageResource(R.drawable.no); 


    return rowView; 
    } 
+0

我在哪裏把這段代碼放在我的代碼中? – user5784363

相關問題