以编程方式更改ListView

时间:2017-07-28 11:23:14

标签: android listview

我有一个带有3个TextView的ListView,每行有一个单选按钮。我从我的firebase数据库中检索某个状态并在单选按钮中显示它。我只是不知道如何访问单选按钮来改变它的状态。我知道我应该使用自定义适配器,但我不知道如何。有人可以帮忙吗?

这是我目前的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grille);
    listView = (ListView) findViewById(R.id._afficher);

    bus = new Bus(ThreadEnforcer.MAIN);

    bus.register(this);

    listView.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false));

    loadData();

}

private void loadData(){
    DatabaseReference mDB = FirebaseDatabase.getInstance().getReference("VFL");

    mDB.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            MatrixCursor matrixCursor= new MatrixCursor(columns);
            startManagingCursor(matrixCursor);

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                for (DataSnapshot snapsht : snapshot.getChildren()){

                    String s = snapsht.child("visiteur").getValue().toString();
                    if(s.equals(visiteur) || s.equals(visiteur1)){

                        matrixCursor.addRow(new Object[] {count.incrementAndGet(), snapsht.child("date").getValue().toString(),
                                snapsht.child("zone").getValue().toString(),snapsht.child("visité").getValue().toString() });

                    }
                }
            }

            bus.post(matrixCursor);

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}
@Subscribe public void answerAvailable( MatrixCursor matrixCursor) {
    // TODO: React to the event somehow!

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.ligne_afficher, matrixCursor, from, to, 0);

    listView.setAdapter(adapter);
}

1 个答案:

答案 0 :(得分:0)

使用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&#39; s活动)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。 listview的CustomAdapter.java类

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);
    }
}

}