I have listview that I have populated into a alertDialog When the alertDialog displays the user are able to click on these items in the listview and that is when the problem comes in. I do not want the user to be able to click on the items in the listview and I have already tried adding this to my xml layout android:clickable="false" and android:focusable="false" but I am still getting the same results. Can somebody assist me with this issue that having.
Here is my base adapter class:
public class MyAdapter extends BaseAdapter {
final String[] listItemsFirstRow = {"Item1","Item2"};
final String[] listItemSecondRow = {"Item1", "Item2"};
#Override
public int getCount() {
return listItemsFirstRow.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.twolinelistview, null);
}
((TextView)convertView.findViewById(R.id.text1)).setText( listItemsFirstRow[position]);
((TextView)convertView.findViewById(R.id.text2)).setText( listItemSecondRow[position]);
return convertView;
}
}
And here is my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="15dip"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false" />
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false" />
</LinearLayout>
Modify your BaseAdapter to override isEnabled to tell the ListView that the items aren't clickable.
Example:
#Override
public boolean isEnabled(int position) {
return false;
}
Note that the documentation says this will do exactly what you want:
Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item)
Try setting android:clickable="false" and android:longClickable="false" for your ListView in XML, or you can set it before you set the adapter as well. That should be enough to disable it, but if not android:focusable="false" and android:enabled="false" should definitely do it.
If you are just trying to prevent items from being highlighted another option is to change how it displays selected items and ignoring the events. One way that was suggested in an older question is:
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
Related
let's say that i have a list of items and i want to build a form with each of these items. This form consist of two checkboxes and an editText. For example i want to know if each item is present in a warehouse and its quantity. I'm thinking for solving my problem to use a listview where each element of my listview will consist of the name of an item, two checkboxes and an editText.
The problem is that the only use of listview i know to present list of elements, i don't how to solve my problem with it (i'm a beginner in android). Can someone help me ?
Is there another way to solve my problem ? Thank you
Try to implements a cusom ListView adapter! This is easier than you might think!
First you need to create layout which would will represent each item in your list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test TEST" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="#id/itemTextView"
android:layout_alignParentRight="true">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/doneCheckBox" />
</LinearLayout>
Then implement cusom adapter inside your code:
public CusomAdapter(Context mainContex, YourItems<SomeItem> someItems) {
this.mainContex = mainContex;
this.someItems = someItems;
}
#Override
public int getCount() {
return someItems.size();
}
#Override
public Object getItem(int position) {
return someItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
if (item == null) {
item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null); // your listView layout here!
}
//fill listView item with your data here!
//initiate your check box
CheckBox doneCheckBox = (CheckBox)item.findViewById(R.id.doneCheckBox);
//add a checkbox listener
doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
doneCheckBox.ischecked=true;
}
else{
doneCheckBox.ischecked=false;
}
}
});
return item;
}
don't forget to add ListView element inside your Activity layout!
I have a ListView in one of my activities that I have bound to an ArrayList using a custom ArrayAdapter. I have set an OnItemClickListener to the ListView which should call a method that starts another activity. However, I find that when I click on the ListView items, it only sometimes works. Sometimes it will start the activity as it should; other times it seems to detect the click (the ripple effect appears on the list item) but does nothing; other times it doesn't even appear to detect the click (the ripple effect doesn't appear).
I've tried all the usual suggestions that I've come across: blocking descendants on the parent view item, setting clickable and focusable to false on all the components of the item views, setting isEnabled to return true in the custom adapter, etc, but the behavior remains the same. Any help appreciated. Here is the relevant code:
Activity containing the ListView:
public class ViewCollectionActivity extends AppCompatActivity {
private final String className = this.getClass().getSimpleName();
private CollectionHandler collectionHandler;
private Context context;
private ArrayList<Game> displayedCollection;
private GameCollectionAdapter collectionAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_collection);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
context = this;
collectionHandler = CollectionHandler.getInstance(this);
TextView view = null;
if (collectionHandler.getDisplayedCollection().size() > 0) {
view = (TextView) findViewById(R.id.no_items_textview);
view.setVisibility(View.GONE);
}
String currentDate = collectionHandler.getDateLastSynchronised();
view = (TextView) findViewById(R.id.last_updated_textview);
view.setText("Last synchronised: " + currentDate + " Total games: " + String.valueOf(collectionHandler.getDisplayedCollection().size()));
collectionAdapter = collectionHandler.getCollectionAdapter();
ListView listView = (ListView) findViewById(R.id.collection_list_view);
listView.setAdapter(collectionAdapter);
AdapterView.OnItemClickListener collectionItemClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
launchGameDetailsActivity(position);
}
};
listView.setOnItemClickListener(collectionItemClickListener);
}
public void launchGameDetailsActivity(int position){
Log.d(className,"Starting lauchGameDetailsActivity method");
collectionHandler.setSelectedGame(position);
Intent intent = new Intent(this,ViewGameDetailsActivity.class);
startActivity(intent);
Log.d(className, "Ending lauchGameDetailsActivity method");
}
The XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.bleachedlizard.ludome.viewcollection.ViewCollectionActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Synchronise Collection"
android:onClick="synchroniseCollection"/>
<TextView
android:id="#+id/last_updated_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last synchronised: "
android:textAlignment="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display Collection"
android:visibility="gone"
android:onClick="displayCollection"/>
<ListView
android:id="#+id/collection_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/no_items_textview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="You have no items in your collection."
android:textAlignment="center"
android:textSize="20sp"/>
</LinearLayout>
The XML for the item views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/collection_item_layout"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:id="#+id/collection_item_image"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/testimage"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
<TextView
android:id="#+id/collection_item_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="16dp"
android:singleLine="false"
android:textColor="#android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
<TextView
android:id="#+id/collection_item_plays"
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="8dp"
android:textColor="#android:color/darker_gray"
android:text="Plays: 0"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
</LinearLayout>
The code for the custom adapter:
public class GameCollectionAdapter extends ArrayAdapter<Game> {
private ArrayList<Game> collection;
public GameCollectionAdapter(Context context, int resource, ArrayList<Game> collection){
super(context, resource, collection);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout gameView = (LinearLayout) convertView;
LayoutInflater mInflater = LayoutInflater.from(getContext());
if (gameView == null) {
gameView = (LinearLayout) mInflater.inflate(R.layout.collection_item_view, null);
}
//Game game = collection.get(position);
Game game = super.getItem(position);
if (game != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView gameTitle = (TextView) gameView.findViewById(R.id.collection_item_name);
TextView numOfPlays = (TextView) gameView.findViewById(R.id.collection_item_plays);
ImageView thumbnail = (ImageView) gameView.findViewById(R.id.collection_item_image);
// check to see if each individual textview is null.
// if not, assign some text!
if (gameTitle != null){
gameTitle.setText(game.getTitle());
}
if (numOfPlays != null){
numOfPlays.setText("Plays: " + String.valueOf(game.getNumOfPlays()));
}
if (thumbnail != null){
thumbnail.setImageBitmap(game.getThumbnail());
}
}
// the view must be returned to our activity
return gameView;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
I discovered what was causing the problem: the way I had set up the array that backed the ListView meant that it was downloading and storing the Bitmaps for every element in the array all the time. Once I changed the implementation so that it only downloaded the images as the ListView required them, then that seemed to improve performance and the onClickListener started to work fine.
The implementation I used was the exact same one shown here:
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
I think the issue is due to the position of the item selection whenever you click you have an list position which is passed to your method launchGameDetailActivity(int position) check with log or toast on item click what all the position you are getting do the needful.
Here is my code try this like this if it helps.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(RecipeClass.this, "Position is" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RecipeClass.this, RecipeIngredients.class)
intent.putExtra("position", position);
startActivity(intent);
}
Check your arraylist value also whether they are not null.
I have a ListView in a Fragment containing also some other UI-Elements. Everything works until I call setAdapter on the ListView. In that moment I can debug that the ListView is filled with the elements but immmediately after the whole fragment disappears (including the other UI-Elements). If I set the Adapter 0,1 seconds later, everything works.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_listview, null);
mListView = (ListView)mView.findViewById(R.id.list_items);
TextView emptyView = (TextView)mView.findViewById(R.id.text_empty);
mListView.setEmptyView(emptyView);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// this is the only work around to not make the list view disappear
loadContacts();
}
}, 100);
// if I call loadContacts() here, the Fragment disappears
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person p = mContacts.get(position);
if (pickerInterface != null) {
pickerInterface.pickedContact(p);
}
}
});
return mView;
}
public void loadContacts() {
mContacts = ContactsDataSource.getAllPhoneContacts(getActivity(), true);
mAdapter = new ContactsAdapter(getActivity());
mAdapter.mContacts = mContacts;
mAdapter.isPlainList = true;
// after the following line the Fragment is filled and then immediately disappears
mListView.setAdapter(mAdapter);
}
the ContactsAdapter is very simple:
public class ContactsAdapter extends BaseAdapter {
public List<Person> mContacts = new ArrayList<Person>();
public Activity context;
// Constructor
public ContactsAdapter(Activity c){
context = c;
}
#Override
public int getCount() {
return mContacts.size();
}
public Person getItem(int position){
return mContacts.get(position);
}
#Override
public boolean isEmpty()
{
return mContacts.size() == 0;
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_contact, null);
}
Person person = mContacts.get(position);
TextView tvName = (TextView) convertView.findViewById(R.id.text_name);
tvName.setText(person.getDisplayName());
return convertView;
}
}
I have spent hours debugging and searching for reasons and have no clue. Thanks for any idea.
Edit: XML as following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<ListView
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/line_horizontal"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/text_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:layout_alignParentTop="true"
android:text="#string/label_no_entries"
android:textColor="#color/grey"
android:textSize="21sp" />
<View
android:id="#+id/line_horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#+id/layout_navigation"
android:background="#color/black" />
<RelativeLayout
android:id="#+id/layout_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:id="#+id/text_scrollback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:drawablePadding="10dp"
android:drawableRight="#drawable/backward"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_backward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollback"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/text_scrollback"
android:layout_alignTop="#+id/text_scrollback"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_scrollforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:drawableLeft="#drawable/forward"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_forward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollforward"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/text_scrollforward"
android:layout_alignTop="#+id/text_scrollforward"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_pages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/text_scrollforward"
android:layout_toLeftOf="#+id/text_scrollforward"
android:layout_toRightOf="#+id/text_scrollback"
android:gravity="center"
android:textColor="#color/mediumgrey"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
Where did you initialize your "mListView"?
The time that fragment inflated is "onCreateView()" and general way of getting view instances from layout is done on this method. like below
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, null);
mListView = (ListView)v.findViewById(R.id.list_items);
...
return v;
}
How about trying to initalize your views(ListViews, and others) in "onCreateView"?
I think that the problem is caused from getting view instances before fragment is inflated.
Not sure what your problem is. May be with the custom super class. Otherwise, wild guess here, but try inflating your view like this.
mView = inflater.inflate(R.layout.fragment_listview, container, false);
First of all, storing a context object can be dangerous and should therefore be avoided if possible... You can easily get rid of it in your adapter with following steps (that hopefully also solves your problem):
--> try following constructor for your adapter:
public ContactsAdapter(Context c){
super(c, R.layout.yourlayout_listitem_name;
}
--> and adjust the getView-method to use something like:
final View rowView = View.inflate(viewGroup.getContext(), R.layout.yourlayout_listitem_name, null);
instead of the inflater-if-block. then always return rowView at the end.
and just to be sure I would remove the overriden-method:
#Override
public long getItemId(int position) {
return position;
}
since it is not doing anything useful anyway ;-)
Thank you for all your support. By removing all code and re-assembling step by step I foundt the reason. Just in case someone runs in the same problem one day.
I had an OnScrollListener on the ListView declared in the superclass. In the OnScrollListener I was hiding the navigation Arrows on demand with Visibility.GONE. Even if the NavigationArrows did not affect the layout of the ListView this was the problem. By setting Visibility.INVISIBLE everything works fine. I guess this is related to some re-drawing-issue of the whole layout that does not work.
I've got huge problem that I don't understand. I have got custom ListView with my own adapter. Each row in ListView has two TextViews - one is a title and the second one is invisible at start. It's going to be visible when there is something new in this item.
In my adapter I set title for every row and set second TextView to be visible when it should be. When I run my app it's fine, but when i scroll down and up the list almost every row is changing invisible TextView to be visible!
I don't know why, but I spaculate that this is something with convertView. Can anybody tell me what's going on?
My adapter:
public class MyListAdapter extends BaseAdapter {
#SuppressWarnings("unused")
private Activity activity;
private ArrayList<String> titles;
private LayoutInflater inflater;
public MyListAdapter (Activity activity, ArrayList<String> titles) {
this.activity = activity;
this.titles = titles;
inflater = (LayoutInflater)activity.getLayoutInflater();
}
public int getCount() {
return titles.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.wpis_list_row, null);
}
TextView title = (TextView)vi.findViewById(R.id.movie_title);
title.setText(titles.get(position));
String name = titles.get(position);
if (name.equals("Name 1") || name.equals("Name 2")
|| name.equals("Name 3")) {
TextView news = (TextView)vi.findViewById(R.id.new_sounds);
news.setVisible(0);
}
return vi;
}
}
and my layout for single row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/movie_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:layout_marginLeft="#dimen/list_margin"
android:text="#string/entry"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<TextView
android:id="#+id/new_sounds"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#8C1717"
android:textSize="13sp"
android:text="#string/news"
android:visibility="invisible" />
</LinearLayout>
Sometimes you are setting the visibility of the text view as visible, based on some if condition . Make sure to change the visibility to gone in the else part.I hope this work . Also you are assigning view view= convertview and then doing if( convertview = null) . Assign the view to convert view in the else part of this check.
To make a View invisible in its layout, use android:visibility attribute set to gone or invisible depending on what you want to do.
Programmatically, to change visibility, call the View.setVisibility(VISIBILITY_YOU_WANT) method. There are some constants in View class you can use: View.GONE , View.VISIBLE , View.INVISIBLE.
BTW, read this post to better understand the ListView recycling.
I want to make a listview unselectable and unclickable. I'm talking about the color change that occurs when I click on a list item. The code is given below. I'm a newbie in android so please be kind.
from : listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16px"/>
</LinearLayout>
from : details.java
TestActionAdapter() {
super(TestDetails.this, R.layout.action_list_item, actions);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TestAction action = actions.get(position);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.action_list_item, parent, false);
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(action.getLabel());
TextView data = (TextView) view.findViewById(R.id.data);
data.setText(action.getData());
return view;
}
Check out the answer here:
How to disable list items...
Basically extend ArrayAdapter, and add these 2 functions:
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
If all you want is to prevent all rows highlighting on click just use ListView android:listSelector="#android:color/transparent"
When you return view from getView(..) Method, just put in view.setEnabled(false) before return view .