2016-08-24 81 views
2

我已经使用阵列适配器创建了简单的多选列表视图。我想设置背景颜色特定列表项。但是,假设我选择了2项均值设置背景,并且还自动设置了12位和22位。请提出我的问题。在列表视图中的多选择

代码黑色。

public class MainActivity extends Activity { 

ListView lvCountry; 

ArrayList<Integer> list = new ArrayList<Integer>(); 
String[] country = { "India", "USA", "Russsia", "China", "Pakistan", 
     "Canada", "UK", "arcot", "vellore", "gudiyattam", "arani", 
     "palani", "chennai", "padi", "velacherry", "ambattur", 
     "ambatttur ot", "maduravoyal", "guindy" }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lvCountry = (ListView) findViewById(R.id.listView1); 
    // Array adapter 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
      MainActivity.this, android.R.layout.simple_list_item_1, country); 
    lvCountry.setAdapter(arrayAdapter); 

    // list selection part 
    lvCountry.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // color selection select item 
      arg1.setBackgroundColor(Color.GRAY); 
     } 
    }); 
}} 
+0

你所要做的是,在你的ListView适配器 –

+0

经过这一点,帮你http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html – sushildlh

+0

〜ARG1 .setBackgroundColor(Color.GRAY);〜当项目设置为bgcolor时,他们选择多项请帮忙解决问题并建议我 – wingsraam

回答

1

实际上什么问题是,当你刷卡的ListView的项目是shuffling.to避免这个问题放在列表视图中滚动视图。如果你想要我可以提供的代码示例。

+0

请给我示例代码先生@Sandeep Kumar – wingsraam

+0

检查以下内容post xml文件和java文件 –

+0

检查以下post xml文件和javafile –

0

创建您自己的适配器,其中每个项目具有“选定”标志,默认情况下已清除。点击给定项目的监听器设置标志并强制重画。你应该使用RecuclerView,因为它工作得更快。当视图充气,检查选择标志,并设置背景相应

+0

你没有回答他的问题 – Alexander

0

XML文件

 <ScrollView 
      android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="5dp" 
    android:scrollbars="none" 
    android:visibility="gone"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ListView 

      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:scrollbars="none" 
      android:visibility="visible" /> 
    </LinearLayout> 

</ScrollView> 

Java类

listview.setAdapter(agentRequestAdapter); 

    setListViewHeightBasedOnChildren(lvAgntRqstView); 
     //////////////////////////// 

    private static void setListViewHeightBasedOnChildren(ListView myListView) { 
    try { 
     ListAdapter myListAdapter = myListView.getAdapter(); 
     if (myListAdapter == null) { 
      //do nothing return null 
      return; 
     } 
     //set listAdapter in loop for getting final size 
     int totalHeight = 0; 
     for (int size = 0; size < myListAdapter.getCount(); size++) { 
      View listItem = myListAdapter.getView(size, null, myListView); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 
     //setting listview item in adapter 
     ViewGroup.LayoutParams params = myListView.getLayoutParams(); 
     params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)); 
     myListView.setLayoutParams(params); 
     // print height of adapter on log 
     Log.i("height of listItem:", String.valueOf(totalHeight)); 


    } catch (NullPointerException e) { 

    } 
} 
0

在本答案由bhatt4982,您可以设置属性,使选项可用于选择列表中的多个条目:

首先,您可以在列表视图本身中设置像本示例中的那样

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    listView.setItemCanFocus(false); 

其次,您可以在布局或layout_simple_list_item_1.xml中声明它。 例如:

<ListView 
    ... 
    android:background="@drawable/your_drawable" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"/> 

然后你可以在你的自定义drawable中设置背景。

Source

+0

arg1.setBackgroundColor(Color.GRAY); // this code实现,当特定的项目点击设置bgcolor和自动另一个项目selected.pls帮助 – wingsraam

+0

嗨,先生,我有一个关于在列表视图中的多选的查询在屏幕上我有20个项目添加在列表视图中,但在当前屏幕上只显示7项其他当我滚动列表视图它会按照出现现在我的查询是当我多选择像2,3,6它选择当前屏幕上的项目,但同样的时间后第二页(向下滚动)的项目被选中我不知道(例如:屏幕1有7个项目,但我的选择只有2,3,6那除了屏幕2(同时向下滚动)9,10,13被选中那里我不kw为什么这个项目被选中屏幕2)怎么可能你解决这个问题? – wingsraam