单击时创建带有可选行的ListView /更改ListView行的背景颜色

时间:2014-11-09 16:40:13

标签: java android android-listview selection

问题

我尝试使用可选项创建ListView。我希望能够点击ListView中的项目并让项目在列表中更改颜色,然后继续使用行中的数据执行其他操作。

我正在使用SimpleAdapter

如何制作它,以便当我点击一行时,它会变成另一种颜色,然后当我点击另一行时,选择新行并更改为新颜色,旧行更改恢复正常?

代码

到目前为止,这是我的代码。 DBTools类包含我希望在ListView中组织和处理的所有数据。 getAllReceivers()方法会返回包含所有数据的ArrayListHashMap<String, String>

MainActivity.java:

public class MainActivity extends ListActivity {
    DBTools dbTools = new DBTools(this);

    ArrayList<HashMap<String, String>> receiverList;

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

        receiverList = dbTools.getAllReceivers();
        dbTools.close();
        ListView listView = getListView();
        if(receiverList.size() != 0) {

            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] {"receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath});
            setListAdapter(adapter);
        }
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="My List" />

    </TableRow>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            android:id="@android:id/list" />

</TableLayout>

receiver_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tableRow" >

    <TextView
        android:id="@+id/receiverId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/receiverName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Robotronics" />

    <TextView
        android:id="@+id/fullPath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123.45.678.910:8088/robtrox/find" />


</TableRow>

1 个答案:

答案 0 :(得分:1)

<强>解决方案

这个问题的解决方案非常简单。我们需要在OnItemClickListener添加ListView来收听点击并做出相应的响应。

因此,在onCreate()方法中,一旦您确定数据集不空,您就会想要覆盖onItemClick()侦听点击并改变颜色的方法。您还希望跟踪您为后续步骤选择的项目,因此请在课程顶部添加public int selectionId = -1;。此外,您需要通过致电ListAdapter((SimpleAdapter) getListAdapter()).notifyDataSetChanged()知道您更改了某些内容。

if(receiverList.size() != 0) {
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
            view.setBackgroundColor(Color.RED);
            TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
            selectionId = Integer.valueOf(receiverIdTextView.getText().toString());
            ((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
         }

    });
    SimpleAdapter adapter = getNewAdapter();
    setListAdapter(adapter);

}

大!现在我们有一个工作系统,可以改变你点击的行的颜色。但我们还没有完成。我们需要确保先前的选择更改回正常颜色。

为此,我们将使用覆盖SimpleAdapter的{​​{1}}方法,每当getView()用于绘制其中显示的项目时,都会调用该方法。

它实际上只显示所需的项目 - 您可以看到的项目。它不会渲染屏幕上方或下方的那些。因此,如果ListView中有200个项目,则一次只会呈现5个或6个项目,具体取决于屏幕大小和项目大小。

要覆盖ListView方法,请转到初始化getView()的位置并将代码更改为:

adapter

每次绘制其中一行时,由于SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) { @Override public View getView (int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId); if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) { view.setBackgroundColor(Color.RED); } else { view.setBackgroundColor(Color.WHITE); } return view; } }; 将被调用,getView()将检查当前ListView是否具有您选择的行ID。如果它没有,它会将背景颜色更改为白色。如果是,则将背景颜色更改为红色。

瞧!那就是它!现在,当您单击view中的项目时,您将背景颜色设置为红色。

最终代码

MainActivity.java:

ListView

activity_main.xml中:

public class MainActivity extends ListActivity {
    DBTools dbTools = new DBTools(this);

    ArrayList<HashMap<String, String>> receiverList;

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

        receiverList = dbTools.getAllReceivers();
        dbTools.close();
        ListView listView = getListView();
        if(receiverList.size() != 0) {
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
                    view.setBackgroundColor(Color.RED);
                    TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
                    selectionId = Integer.valueOf(receiverIdTextView.getText().toString());
                    ((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
                 }

            });

            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) {
                @Override
                public View getView (int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
                    if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) {
                        view.setBackgroundColor(Color.RED);
                    } else {
                        view.setBackgroundColor(Color.WHITE);
                    }
                    return view;
                }
            };
            setListAdapter(adapter);
        }
    }
}

receiver_entry.xml

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="My List" />

    </TableRow>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            android:id="@android:id/list" />
</TableLayout>