为什么图像不显示在列表视图中?

时间:2014-06-27 11:12:42

标签: android

这是代码 图像不显示在列表视图中如何解决这个问题?我将uri值设置为bitmap.so有什么问题?更改了数组并且错误解决了bt静止图像没有显示到列表视图中。 MainActivity.java

public class MainActivity extends Activity {
    ListView lv;
    Context context;

    ArrayList prgmName;
    Bitmap bitmap;
    //bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");

    public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
        context=this;
        Bitmap  [] prgmImages ={bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap};
        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private Bitmap getBitmapFromUrl(String src) {
        // TODO Auto-generated method stub
        try {

            URL url =new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input=connection.getInputStream();
            Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
            return mybiBitmap;


         } catch (Exception e) {
            // TODO: handle exception
           e.printStackTrace();
           return null;
         }


    }

}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter{
    String [] result;
    Context context;
 Bitmap[] imageId;
      private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, Bitmap[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;        
             rowView = inflater.inflate(R.layout.list_item, null);
             holder.tv=(TextView) rowView.findViewById(R.id.textView1);
             holder.img=(ImageView) rowView.findViewById(R.id.imageView1);       
//         holder.tv.setText(result[position]);
//         holder.img.setImageBitmap(imageId[position]);         
             if(result != null && result.length > position)
             { 
               holder.tv.setText(result[position]);
             }

             if(imageId != null && imageId.length > 0)
             {
                holder.img.setImageBitmap(imageId[0]); 
             }

             rowView.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });  
        return rowView;
    }

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text=" Your Theaters..." />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
    </LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />


</LinearLayout>

2 个答案:

答案 0 :(得分:0)

可能是因为在将位图加载并保存到您的阵列之前,您调用了适配器。下载图像需要一些时间,您的适配器不会等待它们。看到这个例子,它正在使用图像加载器:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

答案 1 :(得分:0)

日志中是否有NetworkOnMainThreadException

getBitmapFromUrl调用onCreate时,您正在主线程which is prohibited if your application is targeting Honeycomb or later上下载位图。在你的情况下,它将无声地失败,因为你正在捕获异常,但你将得到一个空结果。

由于从网络加载图像非常常见,因此有很多库可以处理这种情况(更像是缓存),例如: Universal-Image-Loaderion

相关问题