将背景颜色添加到各个列表视图元素

时间:2014-06-15 03:00:51

标签: java android listview

我目前正在设计一个用户点击按钮的应用程序,该按钮的文本会添加到列表视图中。以下是activity_main.xmlMainActivity.java

的精简版本

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button 
            android:id="@+id/good"
            android:text="Good"
            android:background="#99CC00"/>

        <Button 
            android:id="@+id/bad"
            android:text="Bad"
            android:background="#FFBB33"/>

        <Button 
            android:id="@+id/ugly"
            android:text="Ugly"
            android:background="#9933CC"/>

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

MainActivity.java

public class MainActivity extends ListActivity {

    public static ArrayList<String> mylist = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listview = (ListView) findViewById(android.R.id.list);
        Button good = (Button) findViewById(R.id.good);
        Button bad = (Button) findViewById(R.id.bad);
        Button ugly = (Button) findViewById(R.id.ugly);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);

        good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylist.add("Good");
                adapter.notifyDataSetChanged();
                listview.setAdapter(adapter);        
            }
        });

        bad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylist.add("Bad");
                adapter.notifyDataSetChanged();
                listview.setAdapter(adapter);        
            }
        });

        ugly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mylist.add("Good");
                adapter.notifyDataSetChanged();
                listview.setAdapter(adapter);        
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这个设计效果很好,但是我想再添加一个东西:点击一个特定的按钮并将该特定的单词添加为listview中的一个元素时,我想这样做元素与按钮具有相同的背景颜色。例如,如果用户点击按钮Good,我希望Good中显示的文字listview具有背景颜色#99CC00。同样,对于Bad,我希望背景颜色为#FFBB33#9933CCUgly。有谁知道如何处理这个问题?

2 个答案:

答案 0 :(得分:0)

您必须为ListView编写自定义适配器。这使您可以完全控制列表中的每个视图。实际上并不难做到:Custom Adapter for List View

魔法发生在getView()中,你必须检查项目的字符串值,并根据它的单词应用背景颜色。

答案 1 :(得分:0)

要实现此功能:

1).you have to make a custom adapter and set it into your listview.
2).in getview method of your custom adapter just get the id of your custom inflator layout.
3). now according to positions you can set the background of your individual items in the listview.

以下是符合您要求的代码示例。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) _scontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.phone_product_display_inflator, parent, false);

        LinearLayout f_l = (LinearLayout) itemView.findViewById(R.id.pr_bg);
        if (position % 3 == 0) {
            f_l.setBackgroundResource(R.drawable.campaignbase_black);
        } else if (position % 3 == 1) {
            f_l.setBackgroundResource(R.drawable.campaignbase_pink);
        } else if (position % 3 == 2) {
            f_l.setBackgroundResource(R.drawable.campaignbase_brown);
        }
          return itemView;
    }

_ context是适配器类的上下文。