check multiple items in list view - android

I use an adapter to load a list view from a content provider. Each item in the list uses the below layout.
<?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="wrap_content">
<LinearLayout
android:id ="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="#+id/txt_chat"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I have set the list item to allow multiple choice mode :
onCreate(){
...
lv_chatMessages = (ListView) findViewById(R.id.listview_chat);
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv_chatMessages.setItemsCanFocus(false);
...
}
But at run time, i am not able to check any of the items in the list view. If i touch the box, the item just gets highlighted momentarily, but no check mark appears.
I can set a default for all the items if i add an attribute to the CheckedTextView :
android:checked="true"
How do i let the user change the check status of each item at runtime?

refer this link...
CheckedTextView
this may help you..
[Edit by Faizal] :
Using the toggle() inside the onItemClickListener did the trick :
lv_chatMessages.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
CheckedTextView ctv = (CheckedTextView) (((ViewGroup) arg1).findViewById(R.id.txt_chat));
ctv.toggle();
}
});

can you add this into list items
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="false"

Since, you are using a custom adapter, you'll have to maintain the status of the checkbox in the getView() method of the custom adapter.
What you could probably do is to add another Boolean field to your ArrayList of Objects which you use to populate the adapter and check that value in the getView() method. to Something like :
if(status) {
checkBox.setChecked(true);
}
Also, update the status variable using OnCheckedChangeListener().
This method will let you toggle status and also ensure that the checkbox status is maintained even when a list item goes out of the view.
Edit 1 : When not using a custom adapter. This is how I did it.
ListView rendererList;
ArrayList<String> friendlyNames;
friendlyNames = new ArrayList<String>();
rendererList = (ListView) dialog.findViewById(R.id.rendererList);
ArrayAdapter rendererSelectionAdapter = new ArrayAdapter<String>(activity,R.layout.custom_list_layout, friendlyNames);
rendererList.setAdapter(rendererSelectionAdapter);
rendererList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
custom_list_layout was the same layout which android provides, however, I had to make slight modifications so I copied the default file and edited.
here is the custom_list_layout :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="#color/white"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textSize="#dimen/dialog_box_button_textsize" />

Related

Android ListView shows empty content

I have a fragment with a listview. The adapter only has one field: the item id.
When I add an item to the list using the adapter, the list shows the item id.
But if I put a simple item id check before (a for-cycle that checks if the item id is equals to one specific id, if so then adds the item id), the list shows an empty row (like it added something) but without showing the item id.
What could be? I'm sure that the "check part" works.
EDIT: I forgot to specify that the item id is a String.
EDIT 2: I added the check part of the code. It loops through list that contains the item ids that I want to add to the other fragment list. item is the item that I'm checking. blueF is the fragment.
for(int i=0; i<list.getCount();i++) {
if(list.getItem(i).equals(item.getId())) {
//send the item to the listview of the fragment
blueF.getArrayAdapter().addItem(item);
break;
}
}
EDIT 3: After some testing, I discovered that the listview inside the fragment have inside the item (I used getCount and also printed the item id), but it isn't showing the item id! My custom adapter have a notifyDataSetChanged() in the "add" method and I've also recalled the notifyDataSetChanged after the call to the add method in the activity...but nothing.
EDIT 4: Probably is some problem with the layout?
I have this layout for the list row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inventory_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/inventory_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:textSize="17sp"
android:typeface="monospace" >
</TextView>
<TextView
android:id="#+id/inventory_item_counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="17sp"
android:typeface="monospace" />
</LinearLayout>
the fragment layout with the listview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listBlueTagView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Your filter code should be done before setting the Data of the Adapter, so the Adapter have only the items that it will show.
From your question, I understood this,
Try this in your main Activity, these are just Pseudo codes there may be some typos.
public class Main extends Activity{
EditText etID; //Your Item ID field.
ListView listView; // Listview in your XML.
int check_ID; // ID which you want to compare with Edit Texts' value.
List<String> arrayList= new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_XML);
etID = (EditText) findViewById(R.id.editTextID); //ITEM_ID editText id in XML
listView = (ListView) findViewById(R.id.your_ListView_ID);
check_ID = 1; // suppose you want to add into your list if user enters 1 in editText as Item ID
if(check_ID = Integer.parseInt(etID.getText().toString())) //get value from EditText then check it with your desired value and on being equal add item to list
// add item here and set it to listView
arrayList.add("Mango");
ArrayAdapter<String> array_Adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
arrayList);
listView .setAdapter(array_Adapter );
} else{
Toast.makeText(getApplicationContext(),"Wrong ITEM ID",Toast.LENGTH_SHORT).show();
}
}
I've resolved the problem.
It was a graphic problem generated in the getView of the adapter. Basically, some elements overwrote the color text of the ID, rendering it invisible.
I set the text color to Color.BLACK and now I can see the ID of the item.

ListView - differentiate between a checkbox selection and a text selection

Given the ListView below, I wanted to perform two different actions depending on whether the user selects the text (create a new activity) or clicks the associated checkbox (add it to a favorites list). Is this possible in with this setup or will I have to use a custom adapter or even a different layout?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, teams));
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String team_name = adapterView.getItemAtPosition(i).toString().trim();
Intent intent = new Intent("blah.blah.blah");
intent.putExtra("team", team_name);
startActivity(intent);
}
});
A
If I am understanding you correctly, your ListView should contain TextBox and CheckBox and TextBox and CheckBox are clickable, not ListView itself.
For this you have to make custom adapter, where you will make listeners for both the TextBox and CheckBox.
public class CustomAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// your_costum_view should contain textbox and checkboc
view = inflater.inflate(R.layout.your_costum_view, null);
// Get your checkbox and textbox and add listeners
TextView textView = (TextView) view.findById(R.id.textView);
textView.setOnClickListener...
Checkbox checkbox=(CheckBox)view.findById(R.id.checkBox);
checkBox.setOnClickListener...
return view;
}
}
your_costum_view layout example
<?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="wrap_content" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a custom adapter will help you keep trace of your views, it will be easier than use a default adapter that you can't control. For references: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom
Using a custom adapter will definitely make your life easier. In the adapter you can make reference to both the checkbox and the textView and add an onClick Listener to each - from there you can also add code to handle each event. I would also suggest using a recyclerView instead of a ListView. It is the new thing in Android 5.0 and it really is easier to use then a regular ListView. Hope this helps:
RecyclerView Help
Set the CheckBox as focusable="false" in your XML layout.
android:focusable="false"
if don't run go to this link and see example because you need a create custom row in list view and set:
Checkbox checkbox=(CheckBox)view.findById(R.id.checkboxID);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});

Textview onClick is being called instead of the Listview onItemClick.

OK I've searched and seen similar issues, tried them and no avail. I have a listView with some elements and I want to click on one element and display a detail somewhere else.
This is my listView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
ListView listView = (ListView) findViewById(R.id.actualStoresList);
Model.initialize();
Vector<String> values = Model.stores;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Log.i("", "I have selected this: " + str_text);
}
});
CustomStringAdapter adapter = new CustomStringAdapter(this, R.layout.my_list_layout, R.id.list_content, values);
listView.setAdapter(adapter);
}
This is the my_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
The list gets displayed correctly. When I click on an element of the list (A textView) it "steals" the click event so nothing happens (the onItemClickListener is attached to the listView, not the TextView).
The textView has an small margin where, if careful, I can click just behind it, in fact, touching the listView. In this case, the event gets fired ok and I see the log.
I've tried to set the TextView android:focusable="false" but still, the TextView is "above" of the listView and always gets the click events.
How can I either make the TextView "transparent" so it actually clicks on the listView, or add a onclickListener to the TextView so I can handle its events?
Thanks!
Alejandro
Setting clickable property of TextView to false should solve this problem. Try this:
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:clickable="false" />
To make items not focusable, do this:
listView.setItemsCanFocus(false);
source
I think this will solve your issue.
Also make sure the
android:textIsSelectable
property is not set to true.

ListView element not clickable

I have a ListView with SimpleCursorAdapter. The layout was using a LinearLayout, but when reading manuals about memory consumption for complex (nested) layouts, I switched to RelativeLayout instead and I've managed to setup the layout exactly as I want.
Now, there's one quirk. In the emulator, I cannot click the list items. It seems as if the elements are one big "no button". However, if I use the emulator arrows and select the listview element (highlights it) and click the button, it works fine.
Why can't I "click" the listview items since I switched to RelativeLayout?
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/locationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Junk text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/numStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/locationName"
android:text="Junk text #1: 117"
android:textSize="10dp" />
<TextView
android:id="#+id/numItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/numStores"
android:text="Junk text #2: 42"
android:textSize="10dp" />
</RelativeLayout>
I even tried android:clickable="true" for RelativeLayout, to no avail.
EDIT
Code for onClick is as follows:
listItems.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int countyID = cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID));
int numItems = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_ITEMS));
int numStores = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_STORES));
String countyName = cursor.getString(cursor.getColumnIndex(LocationDB.C_NAME));
String foo = String.format(TAG + "Clicked ID #%d (%s), stores = %d, items = %d", countyID, countyName, numStores, numItems);
Log.i(LOG_TAG, foo);
// Show the item in a new activity
Intent apan = new Intent(avBase, Browser.class);
apan.putExtra("countyID", countyID);
startActivity(apan);
}
});
EDIT 2
The code is now tested on my phone, and I get the same error; can't click the items when using RelativeLayout.
I faced some issues with this, too, which seemed to be related to the combination of the ListView's on click listeners and those I assigned each individual item in my Adapter.
In my Adapter.getView() I had to make sure the items were configured like:
item.setLongClickable( false );
item.setClickable( false );
item.setOnClickListener( null );
while my ListView had to have its OnItemClickListener and/or its OnItemLongClickListener set.
Turning on clicks on the individual items and on the listview would not work. Maybe that's what's causing your issue, too.
Seems this is a bug that Google ignores: http://code.google.com/p/android/issues/detail?id=3414

Android problem related OnItemClickListener of ListView

I have a ListView in my layout. Here is the layout for list item.
shopitem.xml
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:mode="twoLine">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="44dip"
android:textStyle="bold"
android:lines="1"
android:textColor="#FFFFFF"/>
<ImageView
android:id="#+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dip"
android:layout_alignRight="#android:id/text1"
android:src="#drawable/btnplaypreview"/>
</TwoLineListItem>
Now I want to set some change of image in playBtn OnItemClickListener. For that I am using the following code.
ListView shopActivityListView = (ListView) findViewById(R.id.shopActivityListView);
shopActivityListView.setCacheColorHint(Color.TRANSPARENT);
shopActivityListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.shopitem, trackArr[1]));
shopActivityListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View vItem, int position, long id) {
// TODO Auto-generated method stub
ImageView playBtn = (ImageView) vItem.findViewById(R.id.playBtn);
playBtn.setImageResource(R.drawable.eq12);
}
});
But nothing is happening on itemclick. I have checked that onItemClick method is executing without any exception. But nothing is changing. What is the problem in my code?
The image in any item of Listview cannot change directly as it is controlled by Adapter. Only getView() method of the adapter can set any view in the items.
Therefore, in onItemClick method, we have to set a position specific flag and then call the notifyDataSetChanged() for the adapter. Moreover, a custom adapter is needed, where we have to define the getView() method so that we have set the image depending on the above position specific flag.

Categories

Resources