2017-07-28 56 views
0

的每一行中禁用的單選按鈕的編程變化值我有3個TextViews一個ListView和每行中的單選按鈕。我從我的firebase數據庫中檢索某個狀態並在單選按鈕中顯示它。我只是不知道如何訪問單選按鈕來改變它的狀態。我知道我應該使用一個自定義適配器,但我不知道如何。有人可以幫忙嗎?一個ListView

這是我暫時代碼:

@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); 
} 
+0

使用自定義適配器 – Anil

+0

CustomAdapter.java類@Anil我知道我應該使用自定義適配器,但我從來沒有使用過,所以我不知道如何 –

回答

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的活動)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); 
    } 
} 

} 
+0

這完美的工作。非常感謝你 –