android Multiple selection ListView & Textview
it may help you.
package com.Sample_MultipleSelection;
import java.util.ArrayList;
import android.R.integer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class Sample_MultipleSelectionActivity extends ListActivity {
private MyListAdapter adapter;
ArrayList<String> item_id = new ArrayList<String>();
ArrayList<String> item_name = new ArrayList<String>();
ArrayList<String> item_balance = new ArrayList<String>();
ArrayList<String> item_email = new ArrayList<String>();
ArrayList<String> items = new ArrayList<String>();
private CheckBox chk_main;
boolean flag = false;
boolean[] selection;
ArrayList<String> selection_val;
private Button btn_select;
private CheckBox chk_select;
ViewHolder holder12;
boolean select_all;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_id.add("1");
item_name.add("China");
item_balance.add("4000");
item_email.add("china@gmail.com");
item_id.add("2");
item_name.add("abc");
item_balance.add("4000");
item_email.add("abca@gmail.com");
item_id.add("3");
item_name.add("xyz");
item_balance.add("4000");
item_email.add("xyz@gmail.com");
item_id.add("4");
item_name.add("xyza");
item_balance.add("40070");
item_email.add("xyze@gmail.com");
item_id.add("5");
item_name.add("xyzc");
item_balance.add("1000");
item_email.add("xyz123@gmail.com");
final ViewHolder holder = new ViewHolder();
selection = new boolean[item_id.size()];
selection_val = new ArrayList<String>();
adapter = new MyListAdapter(this);
setListAdapter(adapter);
holder12 = new ViewHolder();
btn_select = (Button) findViewById(R.id.button1);
btn_select.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int len = selection.length;
int cnt = 0;
String selectIds = "";
for (int i = 0; i < len; i++) {
if (selection[i]) {
cnt++;
}
}
for (int i = 0; i < selection_val.size(); i++) {
selectIds = selectIds + " | " + selection_val.get(i);
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "NO Selection", 1)
.show();
} else {
Toast.makeText(
getApplicationContext(),
"Your are Selected " + cnt + " ids. " + " "
+ selectIds, 1).show();
}
}
});
}
public class MyListAdapter extends BaseAdapter {
Context con;
private LayoutInflater layoutinf;
ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
ArrayList<String> items_ = new ArrayList<String>();
public MyListAdapter(
Sample_MultipleSelectionActivity sample_MultipleSelectionActivity) {
con = sample_MultipleSelectionActivity;
}
public int getCount() {
return item_id.size();
}
public Object getItem(int arg0) {
return item_id.size();
}
public long getItemId(int arg0) {
return item_id.get(arg0).hashCode();
}
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View v = arg1;
ViewHolder holder = null;
if (v == null) {
layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutinf.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.chk = (CheckBox) v.findViewById(R.id.checkBox1);
holder.tv_name = (TextView) v.findViewById(R.id.textView1);
holder.tv_bal = (TextView) v.findViewById(R.id.textView2);
holder.tv_email = (TextView) v.findViewById(R.id.textView3);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.chk.setId(arg0);
holder.tv_name.setId(arg0);
holder.chk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
String val = cb.getText().toString();
if (selection[id]) {
cb.setChecked(false);
selection[id] = false;
selection_val.remove("" + val);
} else {
cb.setChecked(true);
selection[id] = true;
selection_val.add("" + val);
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
Log.e("error", "" + e.toString());
}
}
});
holder.chk.setChecked(selection[arg0]);
if(selection[arg0] == true)
{
holder.tv_name.setBackgroundColor(Color.GRAY);
holder.tv_bal.setBackgroundColor(Color.GRAY);
holder.tv_email.setBackgroundColor(Color.GRAY);
}
else
{
holder.tv_name.setBackgroundColor(Color.TRANSPARENT);
holder.tv_bal.setBackgroundColor(Color.TRANSPARENT);
holder.tv_email.setBackgroundColor(Color.TRANSPARENT);
}
holder.chk.setText(item_id.get(arg0));
holder.tv_name.setText("" + item_name.get(arg0));
holder.tv_bal.setText(item_balance.get(arg0));
holder.tv_email.setText(item_email.get(arg0));
return v;
}
}
public class ViewHolder {
private CheckBox chk;
private TextView tv_name;
private TextView tv_bal;
private TextView tv_email;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/chk_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#66cc33" />
<TextView
android:id="@+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Balance"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#66cc33" />
<TextView
android:id="@+id/textView3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Email-Id"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#66cc33" />
</LinearLayout>
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:background="#fff" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="314dp"
android:layout_weight="1" >
</ListView>
<Button
android:id="@+id/button1"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="select" />
</LinearLayout>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000" />
<TextView
android:id="@+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="3dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:padding="3dp"
android:id="@+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:padding="3dp"
android:id="@+id/textView3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
make it right it if it correct.
Android - Multiple selection in ListView
Basically, you can have a hashset that contains only the selected items. The code would look like this
public class MylibmanList extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
HashSet<String> selectedBooks = new HashSet<String>();
//This listener will be used on all your checkboxes, there's no need to
//create a listener for every checkbox.
CompoundButton.OnCheckedChangeListener checkChangedListener = new CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String bookDuration = (String) buttonView.getTag();
if(isChecked){
selectedBooks.add(bookDuration);
}else{
selectedBooks.remove(bookDuration);
}
}
}
public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView title;
TextView artist;
TextView duration;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = inflater.inflate(R.layout.listrow_row, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.artist = (TextView) convertView.findViewById(R.id.artist);
holder.duration = (TextView) convertView.findViewById(R.id.duration);
holder.check = (CheckBox) convertView.findViewById(R.id.check);
holder.check.setOnCheckedChangeListener(checkChangedListener);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> book = new HashMap<String, String>();
book = (HashMap<String, String>) getItem(position);
holder.check.setTag(book.get(ShowList.LIST_KEY_ID));
holder.title.setText(book.get(ShowList.LIST_KEY_NAME));
holder.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holder.duration.setText(book.get(ShowList.LIST_KEY_ID));
boolean bookSelected = false;
if(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){
bookSelected = true;
}
holder.check.setChecked(bookSelected);
return convertView;
}
I've changed your getView
abit. The viewHolder will now be created only once for each view (as it should). Also, if its not too much of a hustle, you should create a class for your book. Something like
Class Book{
String title;
String artist;
Long duration;
}
How can I select multiple textViews from the collection of textViews which is not in listview on long pressing
I assume you want to highlight textview when selected on long press. Try this:
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
boolean selected = !view.isSelected();
view.setSelected(selected);
view.setBackgroundColor(selected ? Color.RED : Color.TRANSPARENT);
return true;
}
});
Selecting/highlighting multiple items in listview with custom adapter - Android
The root
View
of each item you will display in list must implementCheckable
. When implementing this interface also update the drawable state of View. See this answer for how to do that.Set a
<selector>
drawable as background for this rootView
. Which different color/drawables for checked state and normal states.Set
ListView
's choice mode to single choice or multi-choice.In list adapter, provide item views with above created View as parent layout.
Now, ListView
will take care of setting checked/unchecked state on its item views. Also you can call getCheckedItemIds()
or getCheckedItemPositions()
on ListView
to get currently selected items.
Multiple TextViews in the same List Item
This link uses Multiple Texts in a Single Listview...
http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/
Use the Code as follow..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Custom ListView Example" />
<ListView
android:id="@+id/srListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Multiple Items Selected on ListView, selects wrong items
You can use the following adapter to achieve your goal.
Remove android:entries="@array/list_inventario"
in your Listview and add choicemode as follows in your XML.
<ListView
android:id="@+id/list_inv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice">
</ListView>
InventarioFragment
public class InventarioFragment extends Fragment {
private Activity activity;
private ListView listInventario;
private String[] inventatioItems;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity=activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_inventario_list,container,false);
listInventario = (ListView) view.findViewById(R.id.list_inv);
inventatioItems=getResources().getStringArray(R.array.list_inventario);
InverntarioAdapter adapter=new InverntarioAdapter(activity);
listInventario.setAdapter(adapter);
listInventario.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
listInventario.setItemChecked(pos, true);;
}
});
return view;
}
private class InverntarioAdapter extends BaseAdapter{
private LayoutInflater inflator;
public InverntarioAdapter(Context context){
inflator=(LayoutInflater) context.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return inventatioItems.length;
}
@Override
public Object getItem(int position) {
return inventatioItems[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView == null){
convertView=inflator.inflate(android.R.layout.simple_list_item_1, null);
convertView.setBackground(getResources().getDrawable(R.drawable.selector_background));
}
tv=(TextView) convertView.findViewById(android.R.id.text1);
tv.setText(inventatioItems[position]);
if(listInventario.isItemChecked(position)){
//background Color of selected items
convertView.setBackgroundColor(Color.BLUE);
}
else{
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
}
Related Topics
Android - Making Sliding Drawer to Slide from Left-To-Right
Play an Audio Clip Onto an Ongoing Call
Android Unknown Command 'Crunch'
What Is the "Ignoring Innerclasses Attribute" Warning Output During Compilation
How to Change Date Time Format in Android
Sqlite Example Program in Android
Failed to Resolve: Com.Github.Philjay:Mpandroidchart:V2.1.4
Best Way to Manage the Progressdialog from Asynctask
How to Merge Dex - Android Studio 3.0
Retrofit2.0 Gets Malformedjsonexception While the JSON Seems Correct
Trying to Uninstall_Shortcut But Shortcut Won't Go Away
Failed to Connect to Camera Service
Google Play Security Alert - Your App Is Using an Unsafe Implementation of the Hostnameverifier
Why Isn't My App on the List of Apps to Open Txt File
Android Appcompat-V7:21.0.0 Change Material Checkbox Colors
Circular Dependencies Cannot Exist in Relativelayout, Android