如何在ListView上显示颜色和字体

时间:2018-06-09 06:22:45

标签: android firebase listview custom-adapter

如何解决显示ListView的问题,因为当我从firebase DB读取数据并将其显示在{{1}时,我不知道我必须放入哪些数据}}。 以及当数据来自firebase ListView

时如何解决mList变量的问题

CustomListAdapter.java

DB

ViewDatabase.java

public class CustomListAdapter extends ArrayAdapter<String> {
private Context mContext;
private int id;
private List<String> items ;

public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
    super(context,textViewResourceId,list);
    mContext = context;
    id = textViewResourceId;
    items = list ;
}

@Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.textView);

    if(items.get(position) != null )
    {
        text.setTextColor(Color.WHITE);
        text.setText(items.get(position));
        text.setBackgroundColor(Color.RED);
        int color = Color.argb( 200, 255, 64, 64 );
        text.setBackgroundColor( color );

    }

    return mView;
}

}

view_database_layout.xml

public class ViewDatabase extends AppCompatActivity {
private static final String TAG = "ViewDatabase";

//add Firebase Database stuff
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private  String userID;

private ListView mListView;
private List<String> mList ;




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

    mListView = findViewById(R.id.listview);

    mList = new ArrayList<String>();


    //declare the database reference object. This is what we use to access the database.
    //NOTE: Unless you are signed in, this will not be useable.
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();
    FirebaseUser user = mAuth.getCurrentUser();
   // userID = user.getUid();
    userID ="";

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                toastMessage("Successfully signed in.");
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
                toastMessage("Successfully signed out.");
            }
            // ...
        }
    };

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}


private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        DailyInfo uInfo = new DailyInfo();
        uInfo.setHero1(ds.child(userID).getValue(DailyInfo.class).getHero1()); //set the name1
        uInfo.setHero2(ds.child(userID).getValue(DailyInfo.class).getHero2()); //set the name2
        uInfo.setHero3(ds.child(userID).getValue(DailyInfo.class).getHero3()); //set the name3
        uInfo.setHero4(ds.child(userID).getValue(DailyInfo.class).getHero4()); //set the name4
        uInfo.setHero5(ds.child(userID).getValue(DailyInfo.class).getHero5()); //set the name5
        uInfo.setHero6(ds.child(userID).getValue(DailyInfo.class).getHero6()); //set the name6
        uInfo.setHero7(ds.child(userID).getValue(DailyInfo.class).getHero7()); //set the name7
        uInfo.setHero8(ds.child(userID).getValue(DailyInfo.class).getHero8()); //set the name8
        uInfo.setHero9(ds.child(userID).getValue(DailyInfo.class).getHero9()); //set the name9
        uInfo.setHero10(ds.child(userID).getValue(DailyInfo.class).getHero10()); //set the name10
        uInfo.setHero11(ds.child(userID).getValue(DailyInfo.class).getHero11()); //set the name11
        uInfo.setHero12(ds.child(userID).getValue(DailyInfo.class).getHero12()); //set the name12




        //display all the information
        Log.d(TAG, "showData: Hero1: " + uInfo.getHero1());
        Log.d(TAG, "showData: Hero2: " + uInfo.getHero2());
        Log.d(TAG, "showData: Hero3: " + uInfo.getHero3());
        Log.d(TAG, "showData: Hero4: " + uInfo.getHero4());
        Log.d(TAG, "showData: Hero5: " + uInfo.getHero5());
        Log.d(TAG, "showData: Hero6: " + uInfo.getHero6());
        Log.d(TAG, "showData: Hero7: " + uInfo.getHero7());
        Log.d(TAG, "showData: Hero8: " + uInfo.getHero8());
        Log.d(TAG, "showData: Hero9: " + uInfo.getHero9());
        Log.d(TAG, "showData: Hero10: " + uInfo.getHero10());
        Log.d(TAG, "showData: Hero11: " + uInfo.getHero11());
        Log.d(TAG, "showData: Hero12: " + uInfo.getHero12());


        ArrayList<String> array  = new ArrayList<>();
        array.add(uInfo.getHero1());
        array.add(uInfo.getHero2());
        array.add(uInfo.getHero3());
        array.add(uInfo.getHero4());
        array.add(uInfo.getHero5());
        array.add(uInfo.getHero6());
        array.add(uInfo.getHero7());
        array.add(uInfo.getHero8());
        array.add(uInfo.getHero9());
        array.add(uInfo.getHero10());
        array.add(uInfo.getHero11());
        array.add(uInfo.getHero12());

        /*ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        mListView.setAdapter(adapter);*/


        CustomListAdapter adapter = new CustomListAdapter(ViewDatabase.this , R.layout.custom_list , mList);
        ListView mListView= findViewById(R.id.listview);
        mListView.setAdapter(adapter);
    }
}

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}


/**
 * customizable toast
 * @param message
 */
private void toastMessage(String message){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}

custom_list.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="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<TextView
    android:id="@+id/tvUserInfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="User Information"
    android:textAlignment="center"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="25sp"/>

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

</LinearLayout>

DailyInfo.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textSize="20px" 
android:paddingTop="10dip" 
android:paddingBottom="10dip"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

你在哪里获得gm_info

您是否在NPE填写了数据?我没有找到。 mList正在传递空数据。请检查一下。如果你在适配器中获得异常可能是因为没有使ArrayList = false。让它像吼叫一样。

attachToRoot