我觉得这将是一个简单的错误,但我一直在尝试填充HOURS的列表视图。我正在从Firebase检索值(这很好,因为我在日志中看到数据。)然后将该值添加到“数组列表”中。任何帮助都将是惊人的!谢谢大家
我尝试了几种不同的方法(使用string []而不是列表视图等,但是这些都不起作用。
public class ListTaskActivity extends AppCompatActivity {
ListView list;
FirebaseAuth mAuth;
String mCurrentUser;
MyListView adapter;
ArrayList<String> mylist = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_task);
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser().getUid();
retrieveAllTask();
adapter = new MyListView(this, mylist);
list= findViewById(R.id.mListView);
list.setAdapter(adapter);
}
public void retrieveAllTask(){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Daily Task").child(mCurrentUser);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String theTaskName = ds.child("Daily Name").getValue(String.class);
Log.d("Task Name", theTaskName);
mylist.add(theTaskName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);
}
}
public class MyListView extends ArrayAdapter {
private final Activity context;
private ArrayList maintitle = new ArrayList<>();
public MyListView(Activity context, ArrayList<String>maintitle) {
super(context, R.layout.list_row_layout, maintitle);
// TODO Auto-generated constructor stub
this.context=context;
this.maintitle=maintitle;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_row_layout, null,true);
TextView titleText = rowView.findViewById(R.id.title);
titleText.setText((Integer) maintitle.get(position));
return rowView;
};
}
我的布局资源
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:text="list item 1"
android:textStyle="bold"
android:typeface="sans"
android:textColor="@android:color/black"/>
<ImageView
android:id="@+id/deleteTaskImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_delete_forever_black_24dp"
android:tint="@android:color/holo_red_dark"
/>
<ImageView
android:id="@+id/completeTaskImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_check_circle_black_24dp"
android:tint="@android:color/holo_green_light"/>
</RelativeLayout>