AlertDialog with single choice list - I need some items nonclickable - android

I have AlertDialog with single choice list.
I want to put some 'fake' items inside - like labels of following items. I'm using different layout for regular item and for 'label' item. It it OK.
My problem is: How to make labels NON clickable?
Here is my getView code:
// #Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (m_data.get(position).BaseElementType == ElementType.Divider)
{
convertView = m_li.inflate(this.m_groupResurceID, null);
TextView post = (TextView)convertView.findViewById(R.id.text1);
post.setText(m_data.get(position).TypeToString());
post.getClickable();
}
else
{
convertView = m_li.inflate(this.m_itemResurceID, null);
TextView post = (TextView)convertView.findViewById(R.id.text1);
post.setText(m_data.get(position).Header);
ImageView img = (ImageView)convertView.findViewById(R.id.image1);
Drawable dr = m_data.get(position).TypeToIconId();
dr.setColorFilter(BGMapsApp.IconColor, PorterDuff.Mode.SRC_ATOP);
img.setImageDrawable(dr);
}

The answer is so simple!
Just put this to adapter code:
public boolean isEnabled(int position)
{
//return super.isEnabled(position);
return (m_data.get(position).BaseElementType != ElementType.Divider);
}
Now some items become non clickable :)

Related

My onClickListener work inside a getView method. Why?

I was setting the onClickListener for the ListView, and I have set it in each activity at first and it works. For fun I thought of trying to set onClickListener somewhere else to find more then one solution. So I write the onCLickListener in the getView method of the arrayAdapter.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Checking if there is a View present for reusing if not inflate one.
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_view, parent, false);
}
//getting the current position of the word object in the View
final Word currentWord = getItem(position);
//getting the text view resource for setting our desired text.
TextView mivok = listItemView.findViewById(R.id.mivokTranslation);
mivok.setText(currentWord.getmMiwokTrans());
TextView defaultTran = listItemView.findViewById(R.id.defaultTranslation);
defaultTran.setText(currentWord.getmDefaultTrans());
ImageView imageView = listItemView.findViewById(R.id.image);
//Checking if the View has an Image resource, if yes then setting the correct image.
if (currentWord.checkImageResource == 0) {
imageView.setImageResource(currentWord.getmImageResource());
} else {
imageView.setVisibility(View.GONE);
}
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
listItemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(getContext(),currentWord.getmAudioResource());
mediaPlayer.start();
}
});
return listItemView;
}
While doing I though it won't work. Because as far as I have understood the getView method it set the view on the listView when we return the listItemView at th end. But it worked how can a clickListener event response to an activity that is yet to be added.
Inside getView you simply assign a listener to the returned view. Why would it have not worked? It doesn't matter when you assign a listener - it's an object of anonymous class that is "saved" by the view for later use.

How to set gridview within listview item and click on each grid item with uniq posion and click evant

I want to foll list view with image grid date wise.First get Date list of image and then after get image path list of particular date wise image list.and finally I fill list and grid of particular date wise image(No of images and date not fixed),have a look screen shot.
Date wise images display in Grid View
And now my problem is, When I set click listener on image and add check mark image on click then image add in two position, Why I don't know.and second is when I scroll down list view then check mark visible gone.
I want to select multiple image and add check mark image and perform operation like "Delete image" and "Share image"
Like this
I call grid adapter of each list item , look my code tell me what's wrong
Custom List View adapter : get View method
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder viewHolder = new ViewHolder();
DateSortedImageItem item = getItem(position);
if (row == null) {
row = mInflator.inflate(R.layout.row_date_sorted_image_list, parent, false);
viewHolder.tv_date = (TextView) row.findViewById(R.id.tv_row_date);
viewHolder.rowGridView = (GridView) row.findViewById(R.id.gv_row_item);
viewHolder.tv_date.setText(item.getDate());
row.setTag(viewHolder);
gridAdapter = new GridAdapter(context, R.layout.row_sorted_image_gridview_item,
item.getDateSortedImageList(), position);
viewHolder.rowGridView.setAdapter(gridAdapter);
} else {
row.getTag();
}
return row;
}
private class ViewHolder {
TextView tv_date;
GridView rowGridView;
}
and my grid adapter code is :
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = ((Activity) getContext()).getLayoutInflater();
convertView = mInflater.inflate(R.layout.row_sorted_image_gridview_item, parent, false);
}
view = convertView;
final SquareImageView imageView = (SquareImageView) view.findViewById(R.id.iv_test_row_grid_img);
final ImageView iv_checked = (ImageView) view.findViewById(R.id.iv_checked);
final ImageView iv_unChecked = (ImageView) view.findViewById(R.id.iv_unchecked);
view.setTag(arrList.get(position).toString());
File f = new File(arrList.get(position).toString());
System.out.println("arrList size >>" + arrList.size());
Picasso.with(context).load(Uri.fromFile(f)).resize(100, 100).centerCrop().placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_close_black).into(imageView);
I just set Grid View image. And now i set imageview click ivent and add check mark then add check mark event two places and scroll down then visible gone also.
What's wrong hear and how to set click event of image view unique.
Finally I want to design Look like Google "Photos" app.
Suggest me any example or library:
First you should create a POJO as below
public class AlbumItem {
private String headerStr;
private List<ChildItem> children;
//getter - setter....
public class ChildItem {
private String childName;
private boolean isSelected;
//getter - setter.....
}
}
Now use this POJO inside your main adapter.
and in the second adapter handle the selection logic means if isSelected is true then show iv_checked otherwise show iv_unchecked.
Please let me know if you need further explanation.
Hope it will help you.

Add differents items to custom list adapter

I have a List view with a custom adapter (imageviews), in the same activity I have a header and footer, the listView is between those.
What I want is to add a button as the last item of the list view so when you arrive to the last item it appear, i cant add the button outside because it wont scroll
Sorry about my english
Regards
use ListView.addFooterView() to add view as a footer which is visible only in the end of the list:
http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
If you already have a custom adapter class implemented, the solution is rather simple. Based on an xml layout implemented for your list-view rows which contains both a Button and an ImageView, you can hide/display them in the adapter's getView() method based on the index. This is a code sample, which I currently don't have the chance to test and might not be the most efficient solution, but it should give you an idea:
class CustomAdapter extends SimpleAdapter {
[...]
#Override
public int getCount() {
// number of images to be displayed + 1 for the button
return images.length + 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = inflater.inflate(R.layout.row, parent, false);
final ImageView imageView = (ImageView) row.findViewById(R.id.image);
final Button button = (Button) row.findViewById(R.id.button);
if (position == getCount() - 1) {
// The last element
imageView.setVisibility(View.GONE);
// set an OnClickListener on the button or whatever...
} else {
button.setVisibility(View.GONE);
// do your regular ImageView handling...
}
return row;
}
}

Android ListView: Add non-clickable label

I have a list view and a custom adapter for it. It works all perfect but now I want to add something like a non clickable label, if a condition is true. But if the condition is true it should also display the normal list item. And this is my problem I don't know how I can add the label and afterwards the normal item.
I am a beginner and I tried a lot but I didn't get it.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (position == 3){
rowView = inflater.inflate(R.layout.event_date, parent, false);
TextView date = (TextView) rowView.findViewById(R.id.event_date);
// set text
} else {
rowView = inflater.inflate(R.layout.events_list, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.event_title);
// set text
}
return rowView;
}
BaseAdapter has a method called isEnabled() which you can override. You can use that to assign if the specific position is clickable or not.
#Override
public boolean isEnabled(int position) {
if (position == 3) {
return false;
} else {
return true;
}
}
You will also need to declare that not all the items are enabled using areAllItemsEnabled().
#Override
public boolean areAllItemsEnabled() {
return false;
}
If you just need the label to appear beside the list when a certain condition is met you could create a label in the XML for the page and set its visibility.
eg Label a = findViewById(R.id.labela);
a.setVisibility(View.GONE);
put this in the "if statements" you have created to control the label and set the listview to display the value you want and make it clickable or unclickable accordingly.
lv.setEnabled(false);

Unwanted occasional empty item appearing in ListView

I have a ListView in which each row is a TextView, and display a line of text. I'm getting a problem where occasionally an unwanted empty row appears. The empty row goes away once list scrolls past that particular area.
I've verified my list rows contain the correct information by using the following code after pausing the app in the debugger. Nothing in the output shows up empty or null, etc.
for (int i = 0; i<list.getChildCount(); i++) {
System.out.print((TextView) list.getChildAt(i)).getText());
}
This shows the information I expected.
I also checked the data backing my Adapter for empty entries, new lines, etc.
My getView() method inside the Adapter is as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView t;
if (convertView == null) {
convertView = mInflator.inflate(R.layout.single_message_row, null);
t = (TextView) convertView;
t.setMovementMethod(LinkMovementMethod.getInstance());
t.setTextSize(mMsgSize);
}
else {
t = (TextView) convertView;
}
CharSequence text = get(position);
t.setText(text);
return t;
}
Below is an image demonstrating the problem (the area in red):
Try after changing getView method as:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.single_message_row, parent, false);
}
TextView t=(TextView)row.findViewById(R.id.yourtextview);
t.setText("position "+position);
t.setMovementMethod(LinkMovementMethod.getInstance());
t.setTextSize(mMsgSize);
CharSequence text = get(position);
t.setText(text);
return row;
}
It seems the problem was caused by using match_parent for my TextView width in the ListView. Changing it to wrap_content seems to have fixed it.
For an unwanted empty list item occurring in the ListView, I tried this:
List<String> listofItems;
String strlist=listofItems.get(position);
if(strlist.isEmpty())
{
remove(strlist);
}
'position' is what i got as a parameter in View getView method, because i was implementing a custom adapter.
it worked fine for me!

Categories

Resources