开发者

How does generic ListAdapter works?

I am newbie. I'm sorry if i ask ridiculous question.

I want to have generic Adapter class implements ListAdapter . And I have to override some functions of it like getView .

getView function is i think the most important part and it is calling when it is rendering (like asp.net (I think)).

I have written some code in the simplest level below:

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

And this is my generic ListAdapter class:

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 = _ct开发者_StackOverflowx;
    }

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

The thing that i am wondering is how is the mechanism of ListAdapter and if i want to write template with xml in (i think it will be) res->layout how it should be (composite view that i want to write something ImageView and TextView?

Thank you very very much...


It's hard to understand what you mean...but my best bet is, you want to know how to use your own custom xml as a ListItem in your ListActivity....

As you guessed, Create a xml template of your Listitem and store it among your layouts...

You'll need a LayoutInflater...

LayoutInflater mInflater = LayoutInflater.from(context);

Here's a hacked up version of the Sample Code i found 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;
        }

Note: R.layout.list_item_icon_text is the id of your custom layout. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜