gpt4 book ai didi

java - 如何将字符串值传递给适配器类(基本适配器)android JAVA

转载 作者:行者123 更新时间:2023-11-30 00:02:25 28 4
gpt4 key购买 nike

我正在尝试显示我的 firebase 数据库中的所有项目,这些项目的教师 ID 等于当前登录教师的教师 ID,以便在这里获取当前用户的教师 ID,这就是我所做的。 Activity 类:

    package com.example.ronanlina.attendancechecker;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class SubjectSchedules extends AppCompatActivity {

//member vars;
private String mEmail;
private ListView mSchedListView;
private subjSchedAdapter mAdapter;
private DatabaseReference mDatabaseReference;
private TeacherAccount ta;
private Context context;

public TextView idText;
public String mTeacherId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_schedules);

mDatabaseReference = FirebaseDatabase.getInstance().getReference();

idText = (TextView) findViewById(R.id.teachIdText);
mSchedListView = (ListView) findViewById(R.id.schedListView);

//getting the email of the current user
SharedPreferences prefs = getSharedPreferences(MainActivity.ATTENDANCE_PREFS, MODE_PRIVATE);
mEmail = prefs.getString(MainActivity.EMAIL_KEY,null);

context = this;

}


@Override
public void onStart(){
super.onStart();

//Retrieving the Teacher Id with the email of the current user

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
db.child("teacheraccount"); //

Query query = db.orderByChild("Email").equalTo(mEmail);

query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

//transferring datasnaphsot value to TeacherAccount class
ta = dataSnapshot.getValue(TeacherAccount.class);

//assign the mTeacherId w/ TeacherAccount teacher id
mTeacherId = ta.getTeacherId().toString();
Log.d("teacherID",mTeacherId);

}

@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) {

}
});

// Setting the adapter

//Displaying the teacher id value to a textview. (also for checking if datasnaphot did not return null)

idText.setText(mTeacherId);

//setting the adapter instance
mAdapter = new subjSchedAdapter(this, mDatabaseReference,context);

mSchedListView.setAdapter(mAdapter);

mSchedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {

SubjectScheds infoPass = mAdapter.getItem(position);
String sectionTitle = infoPass.sectionname;

Intent intent = new Intent(SubjectSchedules.this, activity_student_list.class);
intent.putExtra("SECTION_TITLE", sectionTitle);
startActivity(intent);
}
});

}

@Override
public void onStop(){
super.onStop();

mAdapter.cleanup();
}

private void showErrorDialog(String message) {
new AlertDialog.Builder(this)
.setTitle("Oops")
.setMessage(message)
.setPositiveButton(android.R.string.ok, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}

}

我的基础适配器类:

    package com.example.ronanlina.attendancechecker;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import java.util.ArrayList;


public class subjSchedAdapter extends BaseAdapter{

private Activity mActivity;
private DatabaseReference mDatabaseReference;
private String mTeacherId;
private ArrayList<DataSnapshot> mSnapshotList;
private Query query;

// child event listener

private ChildEventListener mListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

mSnapshotList.add(dataSnapshot);
notifyDataSetChanged();

}

@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) {

}
};


//constructor

public subjSchedAdapter(Activity activity, DatabaseReference ref, Context context){

mActivity = activity;
//mTeacherId = subjteacherid;
mDatabaseReference = ref.child("subjects");

//getting the text displayed text from textview (teacher id)
query = mDatabaseReference.orderByChild("teacherid").equalTo(((SubjectSchedules)context).idText.getText().toString());

query.addChildEventListener(mListener);

mSnapshotList = new ArrayList<>();
}

每次我在手机上运行程序并启动 Activity 时,它在 ListView 中什么都不显示,但是当我锁定手机然后解锁时,它会显示结果。

编辑:数据库结构

enter image description here

最佳答案

要解决这个问题,请更改以下代码行:

Query query = db.orderByChild("Email").equalTo(mEmail);

Query query = db.child("teacheraccount").orderByChild("Email").equalTo(mEmail);

关于java - 如何将字符串值传递给适配器类(基本适配器)android JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49684856/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com