Android 配接器
本文介紹兩個Adapter:
ArrayAdapter:最簡單的Adapter,只能展現一行文字,
XML檔案:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
代碼:
ListView listView = findViewById(R.id.listView);
String str[] = new String[]{"點我開通會員","我的QQ錢包","我的個性裝扮","我的收藏","相冊"};
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_expandable_list_item_1,str);
效果圖

BaseAdapter:最多使用的Adapter,實際開發中繼承這個類重寫相關方法,
XML檔案:
主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
資料布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/ic_launcher_background"></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="30dp"></TextView>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電話"
android:textSize="20dp"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Java代碼:
主類
因為用到圖片,所以要在drawable檔案夾里添加相應的圖片資源
public class MainActivity extends AppCompatActivity {
Context context = MainActivity.this;
List list = new ArrayList();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
list.add(new Data(R.drawable.tx,"小明","19656565656"));
list.add(new Data(R.drawable.tx,"小紅","18878787878"));
baseAdapter adapter = new baseAdapter(context,list);
listView.setAdapter(adapter);
}
}
配接器
public class baseAdapter extends BaseAdapter {
Context context;
List <Data> list;
public baseAdapter(Context context, List <Data> list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = LayoutInflater.from(context).inflate(R.layout.mydata, null);
TextView textView1 = v.findViewById(R.id.textView1);
TextView textView2 = v.findViewById(R.id.textView2);
ImageView imageView = v.findViewById(R.id.imageView);
imageView.setImageResource(list.get(i).tradmark);
textView1.setText(list.get(i).name);
textView2.setText(list.get(i).phone);
return v;
}
}
資料類
public class Data {
int tradmark;
String name;
String phone;
public Data(int trademark, String name,String phone){
this.tradmark = trademark;
this.name = name;
this.phone = phone;
}
}
效果圖

Through love comes calm, and through calm comes thought.
愛使人平靜,平靜使人思考,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/226266.html
標籤:其他
上一篇:內嵌H5頁面的互動事件
下一篇:自用
