如何在自定义ListView

时间:2015-11-17 21:48:05

标签: android listview colors cell

我在ListView中为每个单元格设置颜色有问题。我使用自定义ListView:

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

我的适配器:

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);

我将项目添加到列表的方法:

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); 

}

我在为每个单元格的文本着色时遇到问题。 这是我的listview的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">

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

适配器实现:

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;
}

}

1 个答案:

答案 0 :(得分:0)

此代码允许您使用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;
    }

请注意,设置颜色后,可能必须刷新ListView的内容。使用listView.getAdapter().notifyDataSetChanged()执行此操作。您也可以使用listView.invalidateViews(),您可以看到差异here

如果您不需要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;
}

编辑:

上面的答案是关于着色所有条目。

要在所选条目中设置TextView-s的颜色,您需要从条目的给定位置获取当前使用的视图。使用this了解如何执行此操作。您可以将给定的函数粘贴到要创建的类中,并使用listView

获得视图后,您可以设置如下颜色:

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);

最后,如果需要,请不要忘记使用notifyDataSetChanged()invalidateViews()进行刷新。