android listactivity和baseadapter

时间:2011-11-22 22:29:05

标签: android imageview listactivity baseadapter

我有一个Listacitivty,我需要在每一行显示图像和文字。

布局如下:

event.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ImageView  android:layout_height="wrap_content"
         android:layout_width="30px"
        android:layout_marginTop="2px" android:layout_marginRight="2px"
        android:layout_marginLeft="2px" android:id="@+id/logoImage"
        >
    </ImageView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/title"
        android:textSize="15px"></TextView>
</LinearLayout>

活动如下:

  public class Activity extends ListActivity{

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        dba=new DatabaseAdapter(this);

        dba.open();

        View header = getLayoutInflater().inflate(R.layout.header, null);
        ListView lv = getListView();
        lv.addHeaderView(header);

        fillOnStart();
    }

    private void fillOnStart(){
        ArrayList<Title> temp = dba.returnTitle();    // this works

        ArrAdapter notes = new ArrAdapter(this, temp);

        Toast.makeText(this, "Size: " +notes.getCount(), Toast.LENGTH_LONG).show();             
        //because this show good size


        setListAdapter(notes);         //i suppose this is a problem

        }

我的适配器看起来像这样:

 private class ArrAdapter extends BaseAdapter{
        private Context mContext;
        private ArrayList<Title> lista;

        public ArrAdapter(Context c, ArrayList<Title> list){
            mContext = c;
            this.lista =list;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return lista.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //  ViewHolder vh =null;
            //Title title= al.get(position);
            LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


            View v = inflater.inflate(R.layout.event, parent, false);

            ImageView imlogo = (ImageView)v.findViewById(R.id.logoImage);
            TextView textTitle = (TextView)v.findViewById(R.id.tytul);

            textTitle.setText("asdasdasdasd");     //i try like that, to make sure that my databaseAdapter do not have blame on it

            convertView.setTag(v);


            return convertView; 
        }
    }

当我启动应用程序时,我看到“......意外停止......” - 我错过了什么?

更新

我不使用logcat。当我运行logcat并运行程序时,表中没有任何内容。也许我对logcat做错了什么?

当我尝试启动logcat时,我收到此消息:

[2011-11-23 10:26:30 - Logcat]device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
    at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:736)
    at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373)
    at com.android.ddmlib.Device.executeShellCommand(Device.java:284)
    at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:527)

好我的listview显示文字。这适用于View v = inflater.inflate(R.layout.event,null);,

但我没有在logoImage中看到图片。

课程标题如下:

public class Title {

    public int id;
    public String tit;
    public Bitmap logo;
    ... and setter and getter      

}

和方法返回Arraylist(在dba中),其标题如下:

   public ArrayList<Title> returnTitle(){ 
            Title t = new Title(1,"aaaaaaaaaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\favicon.ico"));
            Title t1 = new Title(2,"assdsdsdsdaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\icon.ico"));

            ArrayList<Title> art = new ArrayList<Title>(2);
            art.add(t);
            art.add(t1);


            return  art;

        }
另一方面,我可以动态地将图像插入fodler,例如drawable-hdpi,然后插入我的imageLogo?

1 个答案:

答案 0 :(得分:0)

可能在getView中你需要返回“v”本身,而不是使用convertview.setTag(v)。我想如果你看logcat输出,你可能会得到NullPointerException,因为convertview本身将为null。