2

複数の TextView アイテムを含む ListView があります。このリストは実行時に作成され、サイズが異なる場合があります。実行時に生成される float 値に基づいて TextView アイテムの背景を設定したいと思います。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);

最後の行は NullPointerException をスローします。何らかの理由で、listView 内でこの TextView にアクセスできません。実行時まで色がわからない場合、TextView の背景色を動的に設定するにはどうすればよいですか?

4

4 に答える 4

5

カスタムアダプタを作成するだけです。

このリンクを参照してください、

ListViewで色とフォントを変更する方法

于 2012-08-02T05:25:46.103 に答える
2

TextView を次のように見つけるだけです。

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

そして、たとえば、あなたが望むことを何でもしてください:

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

編集

このサイトで「Android カスタム アダプタ」の実装方法を見つけてください。

于 2012-08-02T05:08:13.967 に答える
0

リストアイテムの色を選択的に変更したいと思います。そのためには、独自のカスタムアダプターを作成し、getView()メソッドをオーバーライドする必要があります。getView()内では、位置に応じて任意のアイテムの色を変更できます。

このリンクは、カスタムアダプタの作成に役立つ場合があります。

getView()は次のようになります-

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

お役に立てば幸いです。

于 2012-08-02T05:24:33.040 に答える
0

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

今、

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

これで、クリックされたテキストビューは灰色になり、その他は白色になります。

于 2012-08-02T05:11:24.240 に答える