0

我已经使用意图创建了一个列表视图,但是为了改善我的应用程序的用户体验,我如何更改列表视图中所选项目的颜色,我直接使用 ListActivity 类而不是来自调色板的列表视图,所以我需要直接将代码添加到活动这里是我的代码

public class BreakfastListView extends ListActivity {

    static final String[] breakfood = {
        "Strawberry shake",
        "Egg muffin",
        "Morning sundae",
        "Peanut butter pancakes w banana",
        "Dippy eggs w marmite soldiers",
        "Breakfast burrito",
        "Ham and cheese muffin",
        "Bacon, egg and cheese bagel",
        "Yoghurt mashup",
        "Hot cereal mashup"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, breakfood));
        // Setting up the list array above

        }

    protected void onListItemClick(ListView lv, View v, int position, long id){
        super.onListItemClick(lv, v, position, id);
        String openbreakclass = breakfood[position];
        if( "Strawberry shake".equals(openbreakclass))
        {
         Intent selectedIntent = new Intent(v.getContext(), Strawberry_Shake.class );
                               startActivity(selectedIntent);
        } else if("Egg muffin".equals(openbreakclass))
        {
         Intent selectedIntent = new Intent(v.getContext(), Egg_muffin.class );
                                startActivity(selectedIntent);  
        }else if("Morning sundae".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Morning_sundae.class );
            startActivity(selectedIntent);
        }else if("Peanut butter pancakes w banana".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Peanut_butter_pancakes_w_banana.class );
            startActivity(selectedIntent);
        }else if("Dippy eggs w marmite soldiers".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Dippy_eggs_w_marmite_soldiers.class );
            startActivity(selectedIntent);
        }else if("Breakfast burrito".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Breakfast_burrito.class );
            startActivity(selectedIntent);
        }else if("Ham and cheese muffin".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Ham_and_cheese_muffin.class );
            startActivity(selectedIntent);
        }else if("Bacon, egg and cheese bagel".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Bacon_egg_and_cheese_bagel.class );
            startActivity(selectedIntent);
        }else if("Yoghurt mashup".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Yoghurt_mashup.class );
            startActivity(selectedIntent);
        }else if("Hot cereal mashup".equals(openbreakclass)){
            Intent selectedIntent = new Intent(v.getContext(), Hot_cereal_mashup.class );
            startActivity(selectedIntent);
        }

    }
4

1 回答 1

0

第一

我可以问你,如果列表视图中有 100 个项目,你会怎么做?您检查数组中的每个项目?

问题:

如果要更改颜色或字体或其他属性,则必须在适配器中进行。

...
View view=convertedView;
if(view ==null)
    ((TextView)view.findViewById(R.id.title)).setTextColor(Color.BLACK); // or what you want
...

更新

因此,如果您想更改颜色,只需BaseAdapter根据我的答案校正getView方法创建一个扩展(或类似)的类。

于 2013-09-21T20:32:35.117 回答