I am writing code for multiple choice list. User easily select multiple item from list after that they can see how many item selected or not. When user will tap on Show Selected Android version easily can get the data in activity.
MainActivity.java
activity_main.xml
content_main.xml
footer_layout.xml
version_row_layout.xml
AndroidVersionName.java
VersionCustomAdapter.java
MainActivity.java
package com.multiplechoicelistview.example; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String[] androidVersions = {"Cupcake 1.5", "Donut 1.6 ", "Eclair 2.0", "Froyo 2.2", "Gingerbread 2.3", "Honeycomb 3.0", "Ice Cream Sandwich 4.0", "Jelly Bean 4.1", "KitKat 4.4", "Lollipop 5.0", "Marshmallow 6.0"}; private VersionCustomAdapter mVersionCustomAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); displayListView(); } /* *show ListView * */ private void displayListView() { View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false); ArrayList<AndroidVersionName> versionNameArrayList = new ArrayList<>(); int versionSize= androidVersions.length; for (int versionCount = 0; versionCount <versionSize ; versionCount++) { AndroidVersionName androidVersionName = new AndroidVersionName(); androidVersionName.setVersionName(androidVersions[versionCount]); versionNameArrayList.add(androidVersionName); } mVersionCustomAdapter = new VersionCustomAdapter(this, R.layout.version_row_layout, versionNameArrayList); ListView listView = (ListView) findViewById(R.id.lvVersionList); listView.setAdapter(mVersionCustomAdapter); listView.addFooterView(footerView); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { AndroidVersionName androidVersionName = (AndroidVersionName) parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(), "Clicked on Row: " + androidVersionName.getVersionName(), Toast.LENGTH_LONG) .show(); } }); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnSelectedList: StringBuffer responseText = new StringBuffer(); responseText.append("Selected Text...\n"); ArrayList<AndroidVersionName> selectedVersionNameList= mVersionCustomAdapter.getSelectedList(); int selectedListSize=selectedVersionNameList.size(); for (int selectedCount = 0; selectedCount < selectedListSize; selectedCount++) { AndroidVersionName androidVersionName = selectedVersionNameList.get(selectedCount); if (androidVersionName.isSelected()) { responseText.append("\n" + androidVersionName.getVersionName()); } } Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG).show(); break; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout>
content_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffeeeeee" android:gravity="center" android:orientation="vertical" android:paddingTop="?attr/actionBarSize"> <ListView android:id="@+id/lvVersionList" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btnSelectedList" android:layout_width="match_parent" android:onClick="onClick" android:layout_height="wrap_content" android:text="@string/show_selection" /> </LinearLayout>
version_row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="6dip"> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:focusable="false" android:focusableInTouchMode="false" android:textColor="#ff00bb88" /> <TextView android:id="@+id/tvVersionName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@id/checkBox1" android:layout_alignBottom="@id/checkBox1" android:layout_toRightOf="@id/checkBox1" android:text="textview" android:textColor="#FF00FF" /> </RelativeLayout>
AndroidVersionName.java
package com.multiplechoicelistview.example; /** * Created by Anay on 10/31/2015. */ public class AndroidVersionName { private String versionName; private boolean isSelected; public boolean isSelected() { return isSelected; } public void setIsSelected(boolean isSelected) { this.isSelected = isSelected; } public String getVersionName() { return versionName; } public void setVersionName(String versionName) { this.versionName = versionName; } }
VersionCustomAdapter.java
package com.multiplechoicelistview.example; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; /** * Created by Anay on 10/31/2015. */ public class VersionCustomAdapter extends ArrayAdapter<AndroidVersionName> { private ArrayList<AndroidVersionName> mVersionNameArrayLists; LayoutInflater mLayoutInflater; private Context mContext; public VersionCustomAdapter(Context context, int textViewResourceId, ArrayList<AndroidVersionName> versionNameArrayLists) { super(context, textViewResourceId, versionNameArrayLists); this.mVersionNameArrayLists = versionNameArrayLists; this.mContext = context; mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public ArrayList<AndroidVersionName> getSelectedList() { return mVersionNameArrayLists; } private class ViewHolder { TextView versionNameTextView; CheckBox checkBox; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; Log.v("ConvertView", String.valueOf(position)); if (convertView == null) { holder = new ViewHolder(); convertView = mLayoutInflater.inflate(R.layout.version_row_layout, null); holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); holder.versionNameTextView = (TextView) convertView.findViewById(R.id.tvVersionName); holder.checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { CheckBox checkBox = (CheckBox) view; AndroidVersionName androidVersionName = (AndroidVersionName) checkBox.getTag(); androidVersionName.setIsSelected(checkBox.isChecked()); } }); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } AndroidVersionName androidVersionName = mVersionNameArrayLists.get(position); holder.versionNameTextView.setText(androidVersionName.getVersionName()); holder.checkBox.setChecked(androidVersionName.isSelected()); holder.checkBox.setTag(androidVersionName); return convertView; } }
No comments:
Post a Comment