«

安卓LISTVIEW

时间:2024-3-2 18:19     作者:韩俊     分类: Android


适配器

Adapter告诉你的LISTVIEW中放啥?是怎么样的
`android
package com.example.Adaper;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ui_qq_login.R;
import com.exaple.person.*;
public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Person> list;

    public ListViewAdapter(Context context, ArrayList<Person> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
    //list_item放置每个标签的格式。
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item,
                null);
        TextView textView_name = (TextView) convertView.findViewById(R.id.name);
        TextView textView_number = (TextView) convertView
                .findViewById(R.id.number);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.pic);
        textView_name.setText(list.get(position).id);
        textView_number.setText(list.get(position).name);
        imageView.setImageDrawable(context.getResources().getDrawable(
                R.drawable.ic_launcher));
        return convertView;
    }

}

## list_item ##

在写LISTVIEW_LAYOUT

package com.example.ui_qq_login;

import java.util.ArrayList;

import com.example.Adaper.*;
import com.example.service.Myservice2;
import com.exaple.person.Person;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ListViewActivity extends Activity {

    public ListView listview;
    public ArrayList<Person> list;
    private Myservice2 myservice2;
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            myservice2 = null;

        }

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder service) {
            myservice2 = ((Myservice2.Mybind) service).getService();
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_listview);

        listview = (ListView) findViewById(R.id.mylistview);
        docreate();
        ListViewAdapter adapter = new ListViewAdapter(ListViewActivity.this,
                list);
                //如果有HEADVIEW,一定要在SETADAPTER之前加入
        listview.setAdapter(adapter);

    }
//以下为创建数据。。项目中都是从服务器拿的数据
    public void docreate() {
        list = new ArrayList<Person>();
        for (int i = 0; i < 20; i++) {
            Person person = new Person(i + "", System.currentTimeMillis() + "");
            list.add(person);
        }
    }

}

标签: android

热门推荐