0

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

布局如下所示:

事件.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)

好的,我的列表视图显示文本。这有效 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 中吗?

4

1 回答 1

0

可能在 getView 中您需要返回“v”本身而不是使用 convertview.setTag(v)。我认为如果您观察 logcat 输出,您可能会收到 NullPointerException,因为 convertview 本身将为空。

于 2011-11-23T09:50:34.783 回答