尝试使用自定义适配器将firebase数据库中的所有数据加载到四个不同的textview中。让它与一个textview一起工作是相当直接的。
使用自定义适配器使其工作变得更加困难。代码运行,但界面中没有显示任何内容,无法解释我们的原因,因为它也没有产生任何错误。有关为什么这不会产生错误或不显示数据的任何想法?
记录模型:
public class Record {
public String clicked;
public String date;
public String diff;
public String sync;
public String uid;
public Record(){
}
public String getClicked() {
return clicked;
}
public String getDate() {
return date;
}
public String getDiff() {
return diff;
}
public String getSync() {
return sync;
}
public String getUid() {
return uid;
}
public void setClicked(String clicked){
this.clicked = clicked;
}
public void setDate(String date){
this.date = date;
}
public void setDiff(String diff){
this.diff = diff;
}
public void setSync(String time){
this.sync = sync;
}
public void setUid(String uid){
this.uid = uid;
}
public Record(String sync, String clicked, String diff, String date, String uid) {
this.clicked = clicked;
this.date = date;
this.diff = diff;
this.sync = sync;
this.uid = uid;
}
CustomAdapter:
public class CustomAdapter extends BaseAdapter{
Context c;
ArrayList<Record> records;
public CustomAdapter(Context c, ArrayList<Record> records) {
this.c = c;
this.records = records;
}
@Override
public int getCount() {
return records.size();
}
@Override
public Object getItem(int position) {
return records.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView= LayoutInflater.from(c).inflate(R.layout.list_adapter_view,parent,false);
}
TextView timeTxt= (TextView) convertView.findViewById(R.id.texttime);
TextView clickedTxt= (TextView) convertView.findViewById(R.id.textclick);
TextView diffTxt= (TextView) convertView.findViewById(R.id.textdiff);
TextView dateTxt= (TextView) convertView.findViewById(R.id.textdate);
final Record s= (Record) this.getItem(position);
timeTxt.setText(s.getSync());
clickedTxt.setText(s.getClicked());
diffTxt.setText(s.getDiff());
dateTxt.setText(s.getDate());
return convertView;
}
}
的活动:
public class UserProfileActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private String mUserId;
CustomAdapter adapter;
DatabaseReference db;
ListView lv;
ArrayList<Record> records = new ArrayList<>();
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile2);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
if (mFirebaseUser == null) {
loadLogInView();
} else {
mUserId = mFirebaseUser.getUid();
lv = (ListView) findViewById(R.id.listView);
db = FirebaseDatabase.getInstance().getReference().child("users").child(mUserId).child("records");
adapter = new CustomAdapter(this, retrieve());
lv.setAdapter(adapter);
}
}
public ArrayList<Record> retrieve()
{
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return records;
}
private void fetchData(DataSnapshot dataSnapshot)
{
for (DataSnapshot ds : dataSnapshot.getChildren()){
records.clear();
Record record= dataSnapshot.getValue(Record.class);
records.add(record);
}
}
activity_user_profile2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5sp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
</RelativeLayout>
list_adapter_view.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"
android:paddingTop="5sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<TextView
android:textStyle="bold"
android:paddingTop="20sp"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/texttime"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<TextView
android:textStyle="bold"
android:paddingTop="20sp"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textclick"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<TextView
android:textStyle="bold"
android:paddingTop="20sp"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textdiff"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<TextView
android:textStyle="bold"
android:paddingTop="20sp"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textdate"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
</LinearLayout>
答案 0 :(得分:1)
问题是Activity类中的几行不在正确的位置。这是工作解决方案。
public class UserProfileActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private String mUserId;
CustomAdapter adapter;
DatabaseReference db;
ListView lv;
ArrayList<Record> records = new ArrayList<>();
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile2);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
if (mFirebaseUser == null) {
loadLogInView();
} else {
mUserId = mFirebaseUser.getUid();
lv = (ListView) findViewById(R.id.listView);
db = FirebaseDatabase.getInstance().getReference().child("users").child(mUserId).child("records");
retrieve();
}
}
public ArrayList<Record> retrieve()
{
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return records;
}
private void fetchData(DataSnapshot dataSnapshot)
{
Record record= dataSnapshot.getValue(Record.class);
records.add(record);
adapter = new CustomAdapter(this,records);
lv.setAdapter(adapter);
}
private void loadLogInView() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
mFirebaseAuth.signOut();
loadLogInView();
}
return super.onOptionsItemSelected(item);
}
}