关于BaseAdapter和simpleAdapter

好长时间没弄安卓了,确实生疏了。突来奇想弄了win10就不说之间为了android studio所做的麻烦事儿了。感觉安卓东西太多学不过来,循序渐进的想法让我搜了搜import是什么

答:用简单的说就是导入,java有很多方法你可以直接使用,但是被封装在各个包(类)里面,你需要先导包,才能使用这个包里的方法,比如说sort(int[ ] a) 这个方法是将int[]数组按照升序,降数组的数排列,但是你必须要先导入import java.util.Arrays;大概就是这个意思,当然也可以是导入你自己的包,或者你自己写的其他类里面的方法(类似include加入头文件,才能利用里面的函数,还可以用户自定义头文件)

今天下午弄了SimpleAdapter和BaseAdapter,下面写写这两个组件吧。

BaseAdapter:

ListView myList;//声明一个ListView
myList = (ListView) findViewById(R.id.myList);//然后通过查找ID确定这个组件用myList来使用这个组件及其类方法


下面编辑BaseAdapter用到的几个类方法:getCount();getItem();getItemid();getView();
public int getCount()
{
//该方法的返回值控制Adapter的列表项的个数
return 40;
}

public Object getItem(int position)
{
return null;//设定第position个列表项的内容
}

public long getItemId(int position)
{
return position;//返回值作为第几个列表项的ID
}
getView(int position);//该方法决定第position个出的列表项组件

// 重写该方法,该方法返回的View将作为列表框
@Override
public View getView(int position
, View convertView , ViewGroup parent)
{
// 创建一个LinearLayout,并向其中添加两个组件
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(0);//这里设置水平走向,因为文字在图片的右边

ImageView image = new ImageView(MainActivity.this);//新建图片View
image.setImageResource(R.drawable.ic_launcher);//确定图片

TextView text = new TextView(MainActivity.this);//新建文本View
text.setText("第" + (position +1 ) + "个列表项");//设置显示文字
text.setTextSize(20);//设置显示文字大小
text.setTextColor(Color.RED);//设置显示文字颜色

line.addView(image);//在该线性布局内添加新建的两个组件
line.addView(text);
// 返回LinearLayout实例
return line;
}

补充LinearLayout线性布局知识。(拷贝)
  1.  LinerLayout线性布局:  
  2.     这种布局方式是指在这个里面的控件元素显线性,  
  3.     我们可以通过setOrientation(int orientation)来指定线性布局的显示方式,  
  4.     其值有:HORIZONTAL(0)、VERTICAL(1)。默认为HORIZONTAL。与之相关的我们  
  5.     也可以在布局文件中通过android:orientation来指定。同理,其值也有:horizontal、  
  6.     vertical    
  7.     LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,  
  8.     按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失,  
  9.     不能完全显示。因此垂直方式排列时,每一行只会有一个 widget或者是container,而不管他们有多宽  
  10.     ,而水平方式排列是将会只有一个行高(高度为最高子控件的高度加上边框高度)。  
  11.     LinearLayout保持其所包含的 widget或者是container之间的间隔以及互相对齐  
  12.     (相对一个控件的右对齐、中间对齐或者左对齐)。  




SimpleAdapter:

// 创建一个List集合,List集合的元素是Map
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

for (int i = 0; i < names.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItem.put("desc", descs[i]);

listItems.add(listItem);
}

// 创建一个SimpleAdapter


SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.simple_item, new String[] { "personName", "header" , "desc"}, new int[] { R.id.name, R.id.header , R.id.desc });
ListView list = (ListView) findViewById(R.id.mylist);
// 为ListView设置Adapter
list.setAdapter(simpleAdapter);

代码分析:SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.simple_item, new String[] { "personName", "header" , "desc"}, new int[] { R.id.name, R.id.header , R.id.desc });
第一个参数:this,
第二个参数:listItems确定集合对象,集合中的对象生成一个列表项

第三个参数:R.layout.simple_item。每一个列表项的布局,每个列表项的组件

第四个参数:new String[] { "personName", "header" , "desc"}。该参数决定了提取对象中的哪些值来生成列表项。

第五个参数:new int[] { R.id.name, R.id.header , R.id.desc }该参数int类,该数组内决定了填充的组件,在那些组件里填充内容

































原文地址:https://www.cnblogs.com/yhc04161120/p/4813686.html