'Changing only 1 color for the text in arraylist connected to firebase
I've managed to only change the color for the whole array list, trying to figure out a way to get the array's position and set different color for each row, this is my code that is changing the whole array list color:
package com.ofir.motoinfo;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseListAdapter;
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.ValueEventListener;
import java.util.ArrayList;
import static android.media.CamcorderProfile.get;
public class ktmList extends AppCompatActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.models_list);
mListView = (ListView) findViewById(R.id.models_list);
final ArrayList<bikeDetails> models = new ArrayList<>();
final BikeAdapter bikeAdapter = new BikeAdapter(this,models);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://xxxxxx.firebaseio.com/models");
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
models.add(new bikeDetails(value, ContextCompat.getColor(ktmList.this, R.color.colorAccent)));
bikeAdapter.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) {
}
});
mListView.setAdapter(bikeAdapter);
}
}
Solution 1:[1]
need to override the getView method in your custom adapter class:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextColor(Color.BLUE);
if(position == 0){
textView.setTextColor(Color.CYAN);
}
if(position == 1){
textView.setTextColor(Color.BLACK);
}
if(position == 2){
textView.setTextColor(Color.YELLOW);
}
if(position == 3){
textView.setTextColor(Color.GREEN);
}
if(position == 4){
textView.setTextColor(Color.MAGENTA);
}
if(position > 4){
textView.setTextColor(Color.BLACK);
}
return view;
}
this is just an example, modify it as you need, may also be easy to put into switch statement
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |