码迷,mamicode.com
首页 > 移动开发 > 详细

android适配器+listview

时间:2018-10-11 16:50:42      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:adapter   base   索引   cti   str   技术   rri   bsp   his   

简单 继承BaseAdapter:

技术分享图片

 

Myadapter代码:

public class Myadapter extends BaseAdapter {
    private Context context;
    private String[] ss;
   /* private List<User>list;*/
    public Myadapter(Context context,String[] ss){
        super();
        this.context=context;
        this.ss=ss;
    }
    @Override
    //得到个数
    public int getCount() {
        return ss.length;
    }

    @Override
    //得到所以索引,即i值
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       String str=ss[position];
        TextView textView=new TextView(context);
        textView.setText(str);
       return textView;
    }
}

MianActivity:

public class MainActivity extends AppCompatActivity {
   private String[] ss=new String[]{
            "山西","太原","山东","贵阳","天津","北京","洪湖"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.list_view);
        Myadapter adapter=new Myadapter(this,ss);
       // ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ss);
        listView.setAdapter(adapter);
    }


}

 进阶baseAdatapter:

 

android适配器+listview

标签:adapter   base   索引   cti   str   技术   rri   bsp   his   

原文地址:https://www.cnblogs.com/goodshred/p/9772847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!