android listactivity和baseadapter

发布于 2024-12-17 13:09:43 字数 4418 浏览 5 评论 0原文

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

布局如下所示:

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)

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

I have a Listacitivty, where I need to show a image and text in every row.

The layout looks like this:

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>

The activity looks like this:

  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

        }

and my adapter looks like this:

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

When i start app I see "...stopped unexpectedly..." - what am I missing?

Update:

i don't use a logcat. WHen i run logcat and run program there is nothing in the table. maybe I do something wrong with logcat?

when i try to start logcat i get this messages:

[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)

ok my listview show a text. this works View v = inflater.inflate(R.layout.event, null);,

but i don't see images in logoImage.

class title looks like:

public class Title {

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

}

and method returns Arraylist (in dba) with Title look like that:

   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;

        }

in otherhand, can I dynamically insert a images to fodler e.g. drawable-hdpi, and then insert to my imageLogo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-12-24 13:09:43

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

probably in getView you need to return "v" itself rather than using convertview.setTag(v). I think if you watch you logcat output, you might be getting NullPointerException as convertview itself will be null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文