0

i have problem with setting color per each cell in ListView. Im using custom ListView:

private ArrayList<HashMap<String, String>> list;

my adapter:

public ListViewAdapter adapter;
ListView listView=(ListView) v.findViewById(R.id.listView1);
            list=new ArrayList<HashMap<String,String>>();
            LayoutInflater inflater1 = getActivity().getLayoutInflater();
            View header = inflater1.inflate(R.layout.naglowek_wlasne_spolki, listView, false);
            listView.addHeaderView(header);

my method to adding item to list:

public void add_stock(String name,double actual_price,double amount,double medium_price){
    HashMap<String,String> temp=new HashMap<String, String>();


    temp.put(FIRST_COLUMN, name);
    temp.put(SECOND_COLUMN, Double.toString(actual_price));
    temp.put(THIRD_COLUMN, Double.toString(medium_price);
    temp.put(FOURTH_COLUMN, amount);
    list.add(temp); 

}

I have problem with coloring each cell's text. This is my xml code for this listview:

<?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">

  <TextView 
      android:id="@+id/nazwa_spolki"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:textStyle="bold" />  

  <TextView 
      android:id="@+id/wartosc_akt"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:textStyle="bold"
      android:layout_weight="1" />

  <TextView 
      android:id="@+id/wartosc_kupna"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:textStyle="bold"
      android:layout_weight="0.5" />

  <TextView 
      android:id="@+id/wartosc_calosci"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:textStyle="bold"
      android:layout_weight="1" />

</LinearLayout>

adapter implementation:

public class ListViewAdapter extends ArrayAdapter<HashMap<String, String>> {

public ArrayList<HashMap<String, String>> list;
private Activity activity;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";


public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super(activity, R.layout.lista_wlasnych_spolek, list);
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public HashMap<String, String> getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListViewHolder listViewHolder;
    if(convertView == null){
        listViewHolder = new ListViewHolder();
        convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
        listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
        listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
        listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
        listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
        convertView.setTag(listViewHolder);
    } else {
        listViewHolder = (ListViewHolder) convertView.getTag();
    }

    HashMap<String, String> map=list.get(position);
    listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
    listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
    listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
    listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;
}

public class ListViewHolder {
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
}

}
pjp
  • 157
  • 1
  • 12
  • You want to change the color of the text, or the cell's background color? Also, could you post your implementation of the adapter? – Nedko Nov 17 '15 at 22:05
  • color of the text in cell, i also added implementation of the adapter. – pjp Nov 17 '15 at 22:22
  • Is there a particular reason to use ListViewHolder? Are you using it somewhere elsewhere? I'll be posting an answer soon. – Nedko Nov 17 '15 at 22:46

1 Answers1

0

This code allows you to set the color dynamically with listView.getAdapter().setColor()

    private int mColor = Color.Black.;

    public setColor(int color)
    {
        mColor = color;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListViewHolder listViewHolder;
    if(convertView == null){
        listViewHolder = new ListViewHolder();
        convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
        listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
        listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
        listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
        listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
        convertView.setTag(listViewHolder);
    } else {
        listViewHolder = (ListViewHolder) convertView.getTag();
    }

    HashMap<String, String> map=list.get(position);
    listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
    listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
    listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
    listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));


    TextView textViewFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
    TextView textViewSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
    TextView textViewThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
    TextView textViewFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);

    textViewFirst.setTextColor(mColor);
    textViewSecond.setTextColor(mColor);
    textViewThird.setTextColor(mColor);
    textViewFourth.setTextColor(mColor);

    return convertView;
    }

Note that after setting the color, the contents of the ListView may have to be refreshed. Use listView.getAdapter().notifyDataSetChanged() to do this. You may also use listView.invalidateViews() , you can see the difference here.

In case you don't need ListViewHolder:

private int mColor = Color.Black.;

public setColor(int color)
{
    mColor = color;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListViewHolder listViewHolder;
    boolean firstUse = false;

    if(convertView == null){
        listViewHolder = new ListViewHolder();
        convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
        firstUse = true;
    } 

    TextView textViewFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
    TextView textViewSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
    TextView textViewThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
    TextView textViewFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);

    if(firstUse)
    {
        HashMap<String, String> map=list.get(position);
        textViewFirst.setText(map.get(FIRST_COLUMN));
        textViewSecond.setText(map.get(SECOND_COLUMN));
        textViewThird.setText(map.get(THIRD_COLUMN));
        textViewFourth.setText(map.get(FOURTH_COLUMN));
    }

    textViewFirst.setTextColor(mColor);
    textViewSecond.setTextColor(mColor);
    textViewThird.setTextColor(mColor);
    textViewFourth.setTextColor(mColor);

    return convertView;
}

EDIT:

The answer above is about coloring all of the entries.

To set the color of TextView-s inside a chosen entry, you need to get the currently used view from a given position of the entry. Use this to learn how to do this. You can paste the given function in the class where you are creating and using listView.

After you get the view, you can set the color like this:

LinearLayout root = (LinearLayout)getViewByPosition(position);
    ((TextView) root.findViewById(R.id.nazwa_spolki)).setTextColor(mColor);
    ((TextView) root.findViewById(R.id.wartosc_akt)).setTextColor(mColor);
    ((TextView) root.findViewById(R.id.wartosc_kupna)).setTextColor(mColor);
    ((TextView) root.findViewById(R.id.wartosc_calosci)).setTextColor(mColor);

And finally, don't forget to refresh with notifyDataSetChanged() or invalidateViews() if needed.

Community
  • 1
  • 1
Nedko
  • 496
  • 5
  • 16
  • Ok, it works great! but i still don't know how to change color text in cell I want. – pjp Nov 18 '15 at 00:16
  • Oh, I didn't realize that you want to change a particular entry. I'll update the answer when I get home. – Nedko Nov 18 '15 at 08:47
  • it works, but im trying to update this datas every 30 sec, and i have another problem when i change color of the cells after a while the color restores to defalt value. Do you know why? – pjp Nov 25 '15 at 16:56