问题
我正在尝试使用可选项创建ListVIEw.我希望能够单击ListVIEw中的项目并让项目在列表中更改颜色,然后继续使用行中的数据执行其他 *** 作.
我正在使用SimpleAdapter.
如何制作它,以便当我点击一行时,它会变成另一种颜色,然后当我点击另一行时,新行被选中并更改为新颜色,旧行变回正常?
码
到目前为止,这是我的代码. DBTools类具有我希望在ListVIEw中显示的所有数据,并且需要处理. getAllReceivers()方法返回包含所有数据的HashMap< String,String>的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>
解决方法:
解
这个问题的解决方案非常简单.我们需要在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);}
大!现在我们有一个工作系统,可以改变你点击的行的颜色.但我们还没有完成.我们需要确保先前的选择更改回正常颜色.
为此,我们将使用覆盖SimpleAdapter的getVIEw()方法,每次ListVIEw用于绘制其中显示的项目时都会调用该方法.
它实际上只显示它需要的项目 – 你可以看到的项目.它不会渲染屏幕上方或下方的那些.因此,如果ListVIEw中有200个项目,则一次只渲染5或6个,具体取决于屏幕大小和项目大小.
要覆盖getVIEw()方法,请转到初始化适配器的位置并将代码更改为:
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.如果没有,它会将背景颜色更改为白色.如果是,它会将背景颜色更改为红色.
瞧!而已!现在,当您单击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>
总结 以上是内存溢出为你收集整理的java – 单击时创建一个带有可选行的ListView /更改ListView行的背景颜色全部内容,希望文章能够帮你解决java – 单击时创建一个带有可选行的ListView /更改ListView行的背景颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)