正确的项目没有在列表视图中选择

正确的项目没有在列表视图中选择

本文介绍了正确的项目没有在列表视图中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这为创建具有单选按钮,自定义列表视图。在本教程中,当我们单击列表中的项目,然后按项目改变颜色。

I am using this Tutorial for Creating a custom listview with radio button. In this tutorial when we click the item in the list then color of item change.

这是当我正在测试4.0以上这个code,但低于4.0它不干活正确,我不明白为什么????发生

This is happening when i am testing this code above 4.0 but below 4.0 it is not workin properly I am not understand why????

类Blog.java

Class Blog.java

public class Blog extends Activity {


 ListView listView;

ArrayList< String>arrayList; // list of the strings that should appear in ListView
ArrayAdapter arrayAdapter; // a middle man to bind ListView and array list


  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       setContentView(R.layout.custom);

      listView = (ListView) findViewById(R.id.lstDemo);


      // LIST OF STRINGS / DATA THAT SHOULD APPEAR IN LISTVIEW HERE WE HAVE HARD CODED     IT WE CAN TAKE THIS INPUT FROM USER AS WELL

      arrayList = new ArrayList();
      arrayList.add("India");
      arrayList.add("USA");
      arrayList.add("England");
      arrayList.add("Singapur");
      arrayList.add("China");
      arrayList.add("Canada");
      arrayList.add("Srilanka");
      arrayList.add("SouthAfrica");




       arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_single_choice,arrayList);
       listView.setAdapter(arrayAdapter);


    //  LETS HIGHLIGHT SELECTED ITEMS

    listView.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView arg0, View view, int position,
 long itemId) {


/*
 *  when we click on item on list view we can get it catch item here.
 * so view is the item clicked in list view and position is the position
 * of that item in list view which was clicked.
 *
 * Now that we know which item is click we can easily change the color
 * of text but when we click on next item we we have to deselect the old
 * selected item means recolor it back to default , and then hight the
 * new selected item by coloring it .
 *
 * So here's the code of doing it.
 *
 *
 * */


CheckedTextView textView = (CheckedTextView) view;
for (int i = 0; i < listView.getCount(); i++) {
 textView= (CheckedTextView) listView.getChildAt(i);
 if (textView != null) {
  textView.setTextColor(Color.WHITE);
 }

}
listView.invalidate();
textView = (CheckedTextView) view;
if (textView != null) {
 textView.setTextColor(Color.BLUE);
}

   }
});

    }
}

我的XML视图

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

<ListView
    android:id="@+id/lstDemo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice">

</ListView>

推荐答案

我们展示您的适配器code,以及尽量不改变从setOnItemClickListener列表视图中的项目,而不是改变你的数据,并通知适配器刷新视图

Show us your adapter code as well and try not to change an item of list view from setOnItemClickListener instead change your data and notify adapter to refresh the view.

设置这个为背景,以列表项:

Set this as background to your list item :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:color="@color/black" />
</selector>

这篇关于正确的项目没有在列表视图中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:17