使用CustomAdapter延伸BaseAdapter類見下面的例子:
1.列表視圖活動佈局
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.oxyzen.loc.YourActivity"
android:id="@+id/listView">
</ListView>
2。 ListView的single_item_layout.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tv1"
android:id="@+id/tv1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tv2"
android:id="@+id/tv2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tv3"
android:id="@+id/tv3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"
android:id="@+id/radioButton"/>
</LinearLayout>
3(ListView的活動)YourActivity.java
public class YourActivity extends AppCompatActivity implements ChildEventListener {
ListView listView;
CustomAdapter adapter;
DatabaseReference yourReference;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yourReference =FirebaseDatabase.getInstance().getReference().child("your_reference");
dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
setContentView(R.layout.activity_your);
listView = (ListView)findViewById(R.id.listView);
adapter = new CustomAdapter();
listView.setAdapter(adapter);
dialog.show();
yourReference.addChildEventListener(this);
}
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
dialog.dismiss();
adapter.addItem(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}
4.列表視圖
public class CustomAdapter extends BaseAdapter {
ArrayList<DataSnapshot> values;
CustomAdapter(){
values = new ArrayList<>();
}
@Override
public int getCount() {
return values.size();
}
@Override
public Object getItem(int position) {
return values.get(position);
//you can get the dataSnapshot by interacting your listview items as per position for use in listview's activity.
//You can return null also here.
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataSnapshot snapshot = values.get(position);//use this snapshot to retrieve values for textviews and radiobutton.
OurViewHolder holder =new OurViewHolder(parent.getContext(),parent);
holder.tv1.setText(snapshot.getKey());
holder.tv2.setText("Text for textView 2");
holder.tv3.setText("Text for textView 3");
holder.button.setChecked(true);// manipulate your radio button as per your wish here. I just checked it for example.
return holder.itemView;
}
//Custom method: to notify and update adapter automatically if new child adds in firebase database.
void addItem(DataSnapshot snapshot){
values.add(snapshot);
notifyDataSetChanged();
}
//Custom class: Using OurViewHolder pattern keeps alive java's OOP concept i.e. one object per listview item.
//Inflate your single item's layout in here and find all components of item.
private class OurViewHolder {
View itemView;
TextView tv1,tv2,tv3;
RadioButton button;
OurViewHolder(Context context, ViewGroup parent){
itemView = LayoutInflater.from(context).inflate(R.layout.single_item_layout,parent,false);
tv1=(TextView)itemView.findViewById(R.id.tv1);
tv2=(TextView)itemView.findViewById(R.id.tv2);
tv3=(TextView)itemView.findViewById(R.id.tv3);
button=(RadioButton)itemView.findViewById(R.id.radioButton);
}
}
}
使用自定義適配器 – Anil
CustomAdapter.java類@Anil我知道我應該使用自定義適配器,但我從來沒有使用過,所以我不知道如何 –