通用ListAdapter如何工作?

时间:2010-10-03 10:18:03

标签: android

我是新手。如果我提出荒谬的问题,我很抱歉。

我想要通用的Adapter类实现ListAdapter。我必须覆盖它的一些功能,如getView

getView函数是我认为最重要的部分,它在渲染时调用(如asp.net(我认为))。

我在下面最简单的部分编写了一些代码:

package com.yeni.listAdapter;

import android.app.ListActivity;
import android.os.Bundle;

public class baslangicAktivitesi extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setListAdapter(new NewAdapter(this));
    }
}

这是我的通用ListAdapter类:

package com.yeni.listAdapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class NewAdapter implements ListAdapter {
    private Context ctx;

    public NewAdapter(Context _ctx) {
        super();
        ctx = _ctx;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

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

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

    @Override
    public int getItemViewType(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv = new TextView(ctx);
        tv.setText("This text will be shown");

        LinearLayout lila = new LinearLayout(ctx);
        lila.addView(tv,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));

        return lila;
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean areAllItemsEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return false;
    }
}

我想知道的是ListAdapter的机制是怎样的,如果我想用xml编写模板(我认为它将会是res->layout它应该是什么(我想写的复合视图) ImageView和TextView?

非常感谢你......

1 个答案:

答案 0 :(得分:0)

很难理解你的意思......但我最好的选择是,你想知道如何在ListActivity中使用自己的自定义xml作为ListItem ....

正如您所猜测的,创建Listitem的xml模板并将其存储在您的布局中...

您需要LayoutInflater ...

LayoutInflater mInflater = LayoutInflater.from(context);

这是我发现here

的示例代码的黑客版本
 public View getView(int position, View convertView, ViewGroup parent) {
            ....;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                TextView tvFoo = (TextView) convertView.findViewById(R.id.text);
                ImageView ivBar = (ImageView) convertView.findViewById(R.id.icon);
            }
            // Bind the data efficiently with the holder.
            tvFoo.setText(/* data at position */);
            ivBar.setImageBitmap(/* image at position*/);

            return convertView;
        }

注意:R.layout.list_item_icon_text是自定义布局的ID。 :)