在listview项旁边添加缩略图

时间:2016-01-13 14:54:20

标签: android listview

如何从每个listview项目左侧的drawable文件夹中添加缩略图图片?

我还想计算列表项的数量并在我的textview中显示它们

任何帮助将受到高度赞赏!我坚持这个很长一段时间

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;
    }
}

2 个答案:

答案 0 :(得分: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;
        }
};  

答案 1 :(得分: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;
  }