SimpleAdapter和Baseadapter填充listActivity-android學習之旅()


簡介

SimpleAdapter的功能是能夠為AbsListView提供復雜的數據,需要構造ListView

代碼示例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {
ListView myList = null;
String[] names = new String[]{
"hello","world", "hello","world"
};
String[] desc = new String[]{
"java","c#","python","swift"
};
private int[] imageIds = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0;i<names.length;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("header",imageIds[i]);
map.put("name",names[i]);
map.put("desc",desc[i]);
listItems.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{
"name","desc","header"
},new int[]{R.id.name,R.id.desc,R.id.header});
myList = (ListView) findViewById(R.id.myList);
myList.setAdapter(adapter);
}

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/header"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="20dp"
android:background="#f0f"
android:id="@+id/name"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="14dp"
android:id="@+id/desc"/>


</LinearLayout>
</LinearLayout>

監聽事件

有兩種方式:
1.OnItemClickListener()
2.OnItemSelectorListener()
這兩種方式都是基於AdapterView提供的。

BaseAdapter簡介

使用BaseAdapter可以獲得對Adapter的控制權,創建多少個列表項以及每個列表項的組件都可以在繼承的復寫方法里面定義。

廢話不多說,代碼示例

package peng.liu.testview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return 40;
}

@Override
public Object getItem(int i) {
return null;
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(LinearLayout.VERTICAL);
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text = new TextView(MainActivity.this);
text.setTextSize(20);
text.setTextColor(Color.RED);
text.setText("第"+i+"個");
line.addView(image);
line.addView(text);
return line;
}
};
ListView myList = (ListView) findViewById(R.id.myList);
myList.setAdapter(adapter);
}
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com