【问题标题】:Create a ListView with selectable rows/change background color of ListView rows when clicked创建具有可选行的 ListView/单击时更改 ListView 行的背景颜色
【发布时间】:2014-11-09 16:40:12
【问题描述】:

问题

我正在尝试创建一个带有可选项目的ListView。我希望能够单击ListView 中的一个项目并让该项目在列表中更改颜色,然后继续对行中的数据执行其他操作。

我正在使用SimpleAdapter

如何做到这一点,当我点击一行时,它会变成不同的颜色,然后当我点击不同的行时,新行被选中并更改为新颜色,旧行更改恢复正常了吗?

代码

到目前为止,这是我的代码。 DBTools 类包含我想要在我的ListView 中显示的所有数据,这些数据被组织和处理。 getAllReceivers() 方法返回包含我所有数据的 HashMap<String, String>s 的 ArrayList

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>

【问题讨论】:

    标签: java android android-listview selection


    【解决方案1】:

    解决方案

    这个问题的解决方法很简单。我们需要在我们的ListView 中添加一个OnItemClickListener 来监听点击并做出相应的响应。

    所以,在onCreate() 方法中,一旦你确定你的数据集不为空,你就会想要重写onItemClick() 方法来监听点击并改变颜色.您还需要跟踪为后续步骤选择的项目,因此请在班级顶部添加public int selectionId = -1;。此外,您需要通过调用((SimpleAdapter) getListAdapter()).notifyDataSetChanged()ListAdapter 知道您更改了某些内容。

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

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

    为此,我们将使用覆盖SimpleAdaptergetView() 方法,每次ListView 绘制其中显示的项目时都会调用该方法。

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

    要覆盖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 将检查当前view 是否具有您选择的行的ID。如果没有,它会将背景颜色更改为白色。如果是,它会将背景颜色更改为红色。

    瞧!而已!现在,当您单击ListView 中的项目时,您将背景颜色设置为红色。

    最终代码

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

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2012-06-19
      • 1970-01-01
      • 2012-03-10
      • 2016-01-11
      相关资源
      最近更新 更多