How do you change the background color of a TextView that is inside a ListView Programmatically?

試著忘記壹切 提交于 2019-12-13 01:29:39

问题


I have a ListView that contains several TextView items. This list is created at runtime, and can vary in size. I would like to set the background of a TextView item based on a float value generated at runtime. I am using an ArrayAdapter.

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,ratios));  
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);

The last line throws a NullPointerException. For some reason I cannot access this TextView inside the listView. How am I supposed to set the background color of a TextView dynamically if I don't know the color until runtime?


回答1:


Simply you have create custom Adapter.

Refer this link,

How to change color and font on ListView




回答2:


Simply find the TextView as:

TextView myTextView = (TextView)findViewById(R.id.yourTextViewId);

And do what ever you want for example:

myTextView.setTextColor(color); 
myTextView.setBackgroundColor(color);

EDIT:

Please find on this site how to implement "android custom adapter"?




回答3:


I assume you want to change color of list items selectively. For that you have to write your own custom adapter and override getView() method. Inside getView() you can change the color of any item depending on the position.

This link may help you write a custom adapter.

your getView() should look something like this -

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        StockQuoteView sqView = null;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.stock_quote_list_item, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sqView = new StockQuoteView();
            sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_symbol);
            sqView.quote = (TextView) rowView.findViewById(R.id.ticker_price);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sqView);
        } else {
            sqView = (StockQuoteView) rowView.getTag();
        }

        if(position == 3) {
            rowView.setBackgroundColor(#030303);
        }

        // Transfer the stock data from the data object
        // to the view objects
        StockQuote currentStock = stocks.get(position);
        sqView.ticker.setText(currentStock.getTickerSymbol());
        sqView.quote.setText(currentStock.getQuote().toString());

        return rowView;
    }

Hope that helps.




回答4:


CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<String>{

    Context mContext;
    String list[];
    LayoutInflater mInflater;
    public static HashMap<Integer, String> idList=new HashMap<Integer,String>();

    public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId, objects);

        mContext=context;
        list=objects;
        mInflater=LayoutInflater.from(context);
        for(int i=0;i<list.length;i++){
            idList.put(i,"false");
        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.list_fruit,null);
            holder=new ViewHolder();

            holder.mTextView=(TextView)convertView.findViewById(R.id.mTextViewId);  
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();

        idList.put(position, "true");           

        if(idList.get(position)=="true")
            holder.mTextView.setBackgroundColor(Color.GRAY);
        else
            holder.mTextView.setBackgroundColor(Color.WHITE);

        return convertView;
    }
    class ViewHolder{
        TextView mTextView;
    }
}

list_fruit.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mTextViewId" 
    android:background="#fff"
    android:textColor="#333"
    android:padding="5dip"/>

Now,

setListAdapter(new CustomAdapter(this, R.layout.list_fruit,ratios));  
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);

Now,whichever textview will be clicked,will get GRAY color,and others are WHITE colored.



来源:https://stackoverflow.com/questions/11771280/how-do-you-change-the-background-color-of-a-textview-that-is-inside-a-listview-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!