Android 更改可滚动列表视图上的背景色
我有一个可滚动的列表视图,其中可以选择多行。选中时,背景颜色将变为绿色,如果再次选中,背景颜色将变回其原始颜色 我设法让它工作,但我的问题是,当我滚动,其他行被突出显示绿色,我没有选择。我认为这是因为视图正在被重用/回收,但我不确定如何解决这个问题 代码:Android 更改可滚动列表视图上的背景色,android,listview,Android,Listview,我有一个可滚动的列表视图,其中可以选择多行。选中时,背景颜色将变为绿色,如果再次选中,背景颜色将变回其原始颜色 我设法让它工作,但我的问题是,当我滚动,其他行被突出显示绿色,我没有选择。我认为这是因为视图正在被重用/回收,但我不确定如何解决这个问题 代码: activitycreated上的公共void(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); myDB=新的DatabaseHandler(g
activitycreated上的公共void(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
myDB=新的DatabaseHandler(getActivity());
listViewSchools=(ListView)getView().findViewById(R.id.listViewFromDB);
如果(myDB.getSchoolCount()!=0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected=新布尔值[SchoolsList.size()];
}
PopulateSchoolView();
listViewSchools.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
t_school item=SchoolsList.get(位置);
如果(选择[职位]){
v、 setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[位置]=假;
}
如果(!isSelected[位置]){
v、 setBackgroundColor(getResources().getColor(R.color.green));
isSelected[位置]=真;
}
schoolAdapter.notifyDataSetChanged();
}
});
}
私有void populateSchoolView(){
schoolAdapter=新建SchoolListAdapter();
listViewSchools.setAdapter(学校适配器);
schoolAdapter.notifyDataSetChanged();
}
私有类SchoolListAdapter扩展了ArrayAdapter{
公立学校列表适配器(){
super(getActivity(),R.layout.itemschool\u布局,SchoolsList);
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
视图行=视图;
ViewHolder=新的ViewHolder();
if(行==null){
行=getActivity().GetLayoutFlater().inflate(R.layout.itemschool\u布局,父级,false);
holder.name=(TextView)row.findViewById(R.id.school\u name);
row.setTag(支架);
}
否则{
holder=(ViewHolder)row.getTag();
}
t_school currentSchool=SchoolsList.get(位置);
如果(当前学校!=null){
holder.name.setText(currentSchool.getInfo());
}
返回行;
}
}
公共类视图持有者
{
文本视图名称;
}
编辑
我试图实现getItemViewType()和getViewTypeCount(),但我不确定是否正确。我遵循本教程作为参考:但是我可以使用更多的指导来说明这些方法中缺少了什么,以及在getView()中是否正确使用了它们
编辑代码:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myDB = new DatabaseHandler(getActivity());
if(myDB.getSchoolCount()==0)
Toast.makeText(getActivity().getApplicationContext(), "Please Import Data", Toast.LENGTH_LONG).show();
listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);
registerForContextMenu(listViewSchools);
listViewSchools.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemIndex = position;
return false;
}
});
if (myDB.getSchoolCount() != 0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected = new boolean[SchoolsList.size()];
}
populateSchoolsView();
listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
t_school item = SchoolsList.get(position);
if (isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[position] = false;
}
else if (!isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.green));
isSelected[position] = true;
}
schoolAdapter.notifyDataSetChanged();
}
});
}
private void populateSchoolsView() {
schoolAdapter = new SchoolListAdapter();
listViewSchools.setAdapter(schoolAdapter);
schoolAdapter.notifyDataSetChanged();
}
private class SchoolListAdapter extends ArrayAdapter<t_school> {
public SchoolListAdapter() {
super(getActivity(), R.layout.itemschool_layout, SchoolsList);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return isSelected[position] ? 1 : 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
ViewHolder holder;
int type = getItemViewType(position);
if (row == null) {
holder = new ViewHolder();
switch ((type)) {
case 1:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;
case 0:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;
}
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
t_school currentSchool = SchoolsList.get(position);
if(currentSchool!=null){
holder.name.setText(currentSchool.getSchool_info());
}
return row;
}
}
public class ViewHolder
{
TextView name;
}
activitycreated上的公共void(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
myDB=新的DatabaseHandler(getActivity());
if(myDB.getSchoolCount()==0)
Toast.makeText(getActivity().getApplicationContext(),“请导入数据”,Toast.LENGTH\u LONG.show();
listViewSchools=(ListView)getView().findViewById(R.id.listViewFromDB);
registerForContextMenu(listViewSchools);
listViewSchools.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
LongClickEditedIndex=位置;
返回false;
}
});
如果(myDB.getSchoolCount()!=0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected=新布尔值[SchoolsList.size()];
}
PopulateSchoolView();
listViewSchools.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
t_school item=SchoolsList.get(位置);
如果(选择[职位]){
v、 setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[位置]=假;
}
如果(!isSelected[位置]){
v、 setBackgroundColor(getResources().getColor(R.color.green));
isSelected[位置]=真;
}
schoolAdapter.notifyDataSetChanged();
}
});
}
私有void populateSchoolView(){
schoolAdapter=新建SchoolListAdapter();
listViewSchools.setAdapter(学校适配器);
schoolAdapter.notifyDataSetChanged();
}
私有类SchoolListAdapter扩展了ArrayAdapter{
公立学校列表适配器(){
super(getActivity(),R.layout.itemschool\u布局,SchoolsList);
}
@凌驾
public int getViewTypeCount(){
返回2;
}
@凌驾
public int getItemViewType(int位置){
返回选择的[位置]?1:0;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
视图行=视图;
视窗座;
int type=getItemViewType(位置);
if(行==null){
holder=新的ViewHolder();
开关((类型)){
案例1:
行=getActivity().GetLayoutFlater().inflate(R.layout.itemschool\u布局,父级,false);
holder.name=(TextView)row.findViewById(R.id.school\u name);
打破
案例0:
行=getActivity().GetLayoutFlater().inflate(R.layout.itemschool\u布局,父级,false);
holder.name=(TextView)row.findViewById(R.id.school\u name);
打破
}
row.setTag(支架);
}
否则{
holder=(ViewHolder)row.getTag();
}
t_school currentSchool=SchoolsList.get(位置);
if(cu
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myDB = new DatabaseHandler(getActivity());
if(myDB.getSchoolCount()==0)
Toast.makeText(getActivity().getApplicationContext(), "Please Import Data", Toast.LENGTH_LONG).show();
listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);
registerForContextMenu(listViewSchools);
listViewSchools.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemIndex = position;
return false;
}
});
if (myDB.getSchoolCount() != 0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected = new boolean[SchoolsList.size()];
}
populateSchoolsView();
listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
t_school item = SchoolsList.get(position);
if (isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[position] = false;
}
else if (!isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.green));
isSelected[position] = true;
}
schoolAdapter.notifyDataSetChanged();
}
});
}
private void populateSchoolsView() {
schoolAdapter = new SchoolListAdapter();
listViewSchools.setAdapter(schoolAdapter);
schoolAdapter.notifyDataSetChanged();
}
private class SchoolListAdapter extends ArrayAdapter<t_school> {
public SchoolListAdapter() {
super(getActivity(), R.layout.itemschool_layout, SchoolsList);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return isSelected[position] ? 1 : 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
ViewHolder holder;
int type = getItemViewType(position);
if (row == null) {
holder = new ViewHolder();
switch ((type)) {
case 1:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;
case 0:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;
}
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
t_school currentSchool = SchoolsList.get(position);
if(currentSchool!=null){
holder.name.setText(currentSchool.getSchool_info());
}
return row;
}
}
public class ViewHolder
{
TextView name;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return isSelected[position] ? 1 : 0;
}