如果此图像是位图,如何将图像显示为简单列表视图

时间:2016-06-12 10:53:34

标签: android image listview bitmap

如果此图像是位图

,我如何将图像显示为简单列表视图

此void在位图中加载图像

 private Bitmap loadImageFromStorage(String ImageName)
{
    ContextWrapper cw = new ContextWrapper(mActivity.getApplicationContext());
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File path=new File(directory,ImageName+".jpg");

    try {

        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(path));
        // ImageView img=(ImageView)findViewById(R.id.imgPicker);
        // img.setImageBitmap(b);
        return b ;
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

    return null;
}

这是从我的单个对象到列表的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"
android:layout_height="60dp"
android:padding="5dp"
>
<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"

    android:background="@color/accent_material_dark"
    style="@style/stile"

    android:id="@+id/imageViewID" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toEndOf="@+id/imageViewID">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nome Gruppo"
            android:id="@+id/NomeGruppo"
            android:layout_alignParentTop="false"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#Gruppo"
        android:id="@+id/IDgruppo"

        android:layout_below="@+id/NomeGruppo"

        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:textColor="#6b6f6e" />
</LinearLayout>

这是我没有图片的加载列表视图的旧方法

private void visualizzaRisultati(){

    final List<Map<String, String>> lista = readDb();



    String[] from = {"NomeGruppo", "IDgruppo"}; //dai valori contenuti in queste chiavi
    int[] to = {R.id.NomeGruppo, R.id.IDgruppo};//agli id delle view
    SimpleAdapter adapter = new SimpleAdapter(
            mActivity.getApplicationContext(),
            lista,//sorgente dati
            R.layout.visualizza_ris_ric_gruppo, //layout contenente gli id di "to"
            from,
            to);
    final ListView mylist = (ListView) getActivity().findViewById(R.id.listaGruppi);

    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {

            Intent nuovaPagina = new Intent(mActivity, gruppo.class);
            nuovaPagina.putExtra("nomeGruppo", lista.get(position).get("IDgruppo").toString());
            startActivity(nuovaPagina);
        }
    });
    mylist.setAdapter(adapter);


}

protected List<Map<String, String>> readDb(){
    SQLiteDatabase db;
    db = getActivity().openOrCreateDatabase("utenteDB", Context.MODE_PRIVATE, null);


    Cursor resultSet = db.rawQuery("Select * from gruppo",null);


    List<Map<String, String>> lista = new ArrayList<Map<String, String>>();




    if (resultSet.moveToFirst()) {
        do {

            final String idgruppo = resultSet.getString(0);
            final String nomeGruppo =resultSet.getString(1);



            lista.add(new HashMap<String, String>() {
                {



                    put("NomeGruppo", nomeGruppo);
                    put("IDgruppo", idgruppo);


                }
            });

        } while (resultSet.moveToNext());
    }

    return(lista);

}

我如何升级此方法以将图像加载到listview的布局

1 个答案:

答案 0 :(得分:0)

这是一个例子。注意:这不是教程网站

 private class MyCustomAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return yourDataset.size();
        }

        @Override
        public String getItem(int position) {
            return yourdata;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    //inflate your layout , bind data

    }
    }