gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:34:24 25 4
gpt4 key购买 nike

问题

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

我正在使用 SimpleAdapter .

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

代码

到目前为止,这是我的代码。 DBTools类包含我想要在 ListView 中显示的所有数据组织和照顾。 getAllReceivers()方法返回 ArrayListHashMap<String, String>里面有我所有的数据。

主要 Activity .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>

最佳答案

解决方案

解决这个问题很简单。我们需要将 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);

}

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

为此,我们将使用覆盖 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 中的项目时,您将背景颜色设置为红色。

最终代码

主要 Activity .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>

关于java - 创建具有可选行的 ListView/单击时更改 ListView 行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com