2016-08-24 83 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