TextView with AlerDialog - android

Ok,I'm trying to make a List view that will list some places ,where each item will have a picture(ImageVew ) and a TextView so that when a place gets hit an AlerDialog box will appear with informations(different info for each place).I know how to make the List View...but I don't know how to make it clickable and display an Dialog Window with diff info...also I'll need an adapter.Is it possible to accomplish this?if so how?Than you

In my case the image is a checkbox.
The XML could be like:
<?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">
<CheckBox android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#android:drawable/btn_star"
android:focusable="false"/>
<TextView android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5px"
android:layout_marginTop="6px"
android:layout_toRightOf="#+id/check"
android:textSize="25px"
android:focusable="false"/>
</RelativeLayout>
You need an adapter like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Object np = getItem(position);
View view = null;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.listitem, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
view.setTag(viewHolder);
viewHolder.checkbox.setTag(np);
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(np);
}
final ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(np.toString);
holder.text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
[Create the Dialog]
}
});
holder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
[do something]
}
});
return view;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}

to add an event listener on listview click:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// you create your dialog here
}
});
to create a dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("your_message_here")
.setPositiveButton(getResources().getString(R.string.ok),
dialogClickListener).setCancelable(false).show();

Related

OnItemClick listener does not work with custom adapter with checkbox

I am having custom adapter with item with checkbox and textview. Here is the layout of the item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/standard_margin"
>
<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"
/>
<TextView
android:id="#+id/checkBoxDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="#dimen/new_small_margin"
android:layout_alignBaseline="#+id/isEbank"
android:layout_alignBottom="#+id/isEbank"
android:layout_toRightOf="#+id/isEbank"
android:layout_alignTop="#+id/isEbank"
android:gravity="left"
android:focusable="false"
android:focusableInTouchMode="false"
/>
Here is how the list looks like:
I like when user clicks on list item , or on checkbox to perform same action. However onItemClick does not fire when user clicks on item.
Here is my code for the view:
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_user, container, false);
ArrayList<Option> listOptions=new ArrayList<Option>();
Option optionYes=new Option(true,getResources().getString(R.string.Yes));
listOptions.add(optionYes);
Option optionNo=new Option(false,getResources().getString(R.string.No));
listOptions.add(optionNo);
listView=(ListView)mRoot.findViewById(R.id.lst);
adapter=new OptionAdapter(getActivity(),R.layout.item_option,listOptions);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox box = (CheckBox) view.findViewById(R.id.checkBox1);
box.setChecked(true);
}
});
return mRoot;
}
Here is my code for the adapter:
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
final Option selectedOption = optionsList.get(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_option, null);
holder = new ViewHolder();
holder.optionDesc = (TextView) convertView.findViewById(R.id.optionDesc);
holder.optionDesc.setText(selectedOption.getDesc());
holder.optionCheckBox = (CheckBox) convertView.findViewById(R.id.isEbank);
holder.optionCheckBox.setFocusable(false);
holder.optionCheckBox.setFocusableInTouchMode(false);
convertView.setTag(holder);
holder.optionCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
However it does not work.
you should disable user interaction add this to xml
android:clickable="false"
I suggest you to do this inside getView like here
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.item_option, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
//Check if cb is checked or not here
cb.isChecked();
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
}

Android-CheckBox selection Only from ListView

I was wondering if I can check items (checkbox) from ListView and have something with the selected items. But I dont want to use 'ListView.OnItemClickListener' because I have a separate functionality for clicking the list item. Something like 'CheckBox.OnClickListener' where I can grab all the list items that are checked. Any help is highly appreciated. Below is the code I am currently having, not functional though:
public class MainActivity extends AppCompatActivity {
ListView list;
GoodAdapter imageAdapter;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState) {
//populates the details as in imageBeanArray
imageAdapter = new GoodAdapter(this, imageBeanArray);
list.setAdapter(imageAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
//does something
}
//throws NPE
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isSelected()) {
checkedArray.add((ImageBean) list.getItemAtPosition(list.getSelectedItemPosition()));
}
}
});
}
GoodAdapter.java:
All other variables initialized
public class GoodAdapter extends BaseAdapter{
boolean[] itemChecked;
CheckBox[] checkBoxArray;
public GoodAdapter(Context context, ArrayList<ImageBean> imageBean) {
mInflater = LayoutInflater.from(context);
mImageBeans = imageBean;
itemChecked=new boolean[imageBean.size()];
checkBoxArray=new CheckBox[itemChecked.length];
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = mInflater.inflate(R.layout.good_adapter, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)view.findViewById(R.id.image);
holder.name = (TextView)view.findViewById(R.id.docName);
holder.imageMore = (TextView)view.findViewById(R.id.imageMore);
holder.imageMore.setVisibility(View.GONE);
holder.checkbox=(CheckBox)view.findViewById(R.id.checkBox);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(false);
if (itemChecked[position]){
holder.checkbox.setChecked(true);
mImageBeans.get(position).setSelected(true);
}
else
holder.checkbox.setChecked(false);
}
ImageBean imageBean = mImageBeans.get(position);
Bitmap bm = BitmapFactory.decodeByteArray(imageBean.getImage(), 0, imageBean.getImage().length);
holder.imageView.setImageBitmap(bm);
holder.name.setText(imageBean.getDocName());
if (imageBean.isMoreThanOne()!=0)
holder.imageMore.setVisibility(View.VISIBLE);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkbox.isChecked())
itemChecked[position] = true;
else
itemChecked[position] = false;
}
});
checkBoxArray[position].setChecked(itemChecked[position]);
checkBoxArray[position].setOnCheckedChangeListener(mListener);
return view;
}
CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
itemChecked[(Integer)buttonView.getTag()] = isChecked; // get the tag so we know the row and store the status
}
};
private class ViewHolder {
public ImageView imageView;
public TextView name,imageMore;
public CheckBox checkbox;
}
Good Adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="64dip"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:weightSum="1"
android:id="#+id/imageList">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<ImageView
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_marginLeft="2dp"
android:layout_gravity="center_vertical"
android:layout_width="90dip"
android:layout_height="match_parent"
android:background="#drawable/image_border"
android:padding="4dp"
android:cropToPadding="true"
/>
<TextView
android:id="#+id/docName"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:textColor="#color/black"
android:textSize="20dp"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/imageMore"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:background="#drawable/more"/>
</LinearLayout>
Please help me.I didnt find any useful tutorials/solutions.
From my experience you can't have a listener on component of the row and a listener on the row itself if you do that one will take advantage on the other that will never be called.

How to setup onclick listener for individual row elements?

I have an activity which contains only a listview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="#+id/list_data"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>
</LinearLayout>
Each row of this list is styled by this xml file. Each row has 2 textviews and a button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/playPauseB"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Play" />
<TextView
android:id="#+id/rec_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="20sp"
android:layout_toRightOf="#id/playPauseB" />
<TextView
android:id="#+id/rec_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rec_name"
android:text="Sample Data 2"
android:textSize="15sp" />
</RelativeLayout>
This listview has a cutom cursor adapter. My question is how to setup onClick listener for each button in a row. And how to access buttons of each row? My activity has a DBHelper object and customCursor object.My adapter contains a bindView and newView methods. Please comment if more details are needed.
Simple add click listener within getview(), as you doing in activities.
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pos = (Integer) v.getTag();
Log.d("#####", "tag pos=" + pos);
}
});
You need to set Tag & get Tag to avoid position issue, on click of button.
You can set onClickListener for each row in getView method of your adapter
like this
private View getView(int position, View convertView, ViewGroup parent) {
View row = // initialize your row
Button b = (Button)row.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
})
}
Assuming your cursor adapter extends BaseAdapter;
implement getView method as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
Button playPauseBtn = (Button) view.findViewById(R.id.playPauseB);
playPauseBtn.setOnClickListener(playPauseListener);
}
OnClickListener playPauseListener = new OnClickListener() {
#Override
public void onClick(View view) {
//Do some thing ...
}
};
Change your getView method in CustomAdapter class as below:
#Override
public View getView(final int arg0, View convertview, ViewGroup arg2) {
// TODO Auto-generated method stub
final ViewHolder holder;
if(convertview==null)
{
holder = new ViewHolder();
convertview = layoutinflater.inflate(R.layout.yourXmlName, null);
holder.txt1=(TextView)convertview.findViewById(R.id.rec_desc);
holder.txt2=(TextView)convertview.findViewById(R.id.rec_name);
holder.btn=(Button)convertview.findViewById(R.id.playPauseB);
convertview.setTag(holder);
}
else{
holder=(ViewHolder)convertview.getTag();
}
holder.txt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
holder.txt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
holder.btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
return convertview;
}
static class ViewHolder
{
TextView txt1;
TextView txt2;
Button btn;
}
You can try this
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View hView = convertView;
final ViewHolder holder;
if (convertView == null) {
hView = mInflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.text1 = (TextView) hView.findViewById(R.id.text1);
holder.text2 = (TextView) hView.findViewById(R.id.text2);
holder.button = (Button) hView.findViewById(R.id.button);
hView.setTag(holder);
}else{
holder = (ViewHolder) hView.getTag();
}
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do your work on click...
}
});
return hView;
}
static class ViewHolder {
TextView text1;
TextView text2;
Button button;
}

getView showing wrong images in a custom adapter?

I have a list with a custom adapter and a custom row.
The problem is that when I scroll the listview, the images are at the wrong positions...
Here is the custom row xml:
<LinearLayout
android:id="#+id/imagequality_listview_row_linearlayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:weightSum="1"
android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imagequality_listview_row_textview"
android:layout_width="0dp"
android:layout_height="40dip"
android:gravity="right"
android:paddingLeft="5dip"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:layout_weight="0.6"
/>
<com.example.listviewtest.LoaderImageView
android:id="#+id/imagequality_listview_row_loaderImageView"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="100dp"
image="http://developer.android.com/images/dialog_buttons.png"
/>
</LinearLayout>
Here is the GetView:
List<ImageQuality_Item> items;
Context context;
List<CheckedTextView> checkedList;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.imagequality_listview_row, null);
holder = new ViewHolder();
holder.layout = (LinearLayout) convertView.findViewById(R.id.imagequality_listview_row_linearlayout);
holder.textview = (CheckedTextView) convertView.findViewById(R.id.imagequality_listview_row_textview);
holder.image = (LoaderImageView) convertView.findViewById(R.id.imagequality_listview_row_loaderImageView);
checkedList.add(holder.textview);
holder.layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < checkedList.size(); i++)
if (checkedList.get(i).isChecked())
checkedList.get(i).setChecked(false);
((CheckedTextView) ((LinearLayout) v).getChildAt(0)).setChecked(true);
}
});
holder.textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < checkedList.size(); i++)
if (checkedList.get(i).isChecked())
checkedList.get(i).setChecked(false);
((CheckedTextView) v).setChecked(true);
}
});
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.textview.setText(items.get(position).getDescription());
if (holder.image.used == false) {
holder.image.setImageDrawable(items.get(position).getURL());
holder.image.used = true;
}
return convertView;
}
class ViewHolder {
LinearLayout layout;
LoaderImageView image;
CheckedTextView textview;
}
How can I fix this?
You have to set the Drawable every time getView() gets called, since android is reusing the list elements. It is likely that an earlier set image appears, if you dont update it.
Just remove your condition to set the image drawable, and it will work.

how can i use list activity with controling my checked elements?

I googled and i tried to do this but couldnt implement successly.
My layout file
<?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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list"></ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="buyButtonClickProduct"
android:id="#+id/buyButtonClickProduct"></Button>
</LinearLayout>
I have a custom adapter as :
private final List<Products> list;
private final Activity context;
public ProductsCustomAdapter(Activity context, List<Products> list) {
super(context, R.layout.coposite_product_info, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView productInfoText;
protected CheckBox checkbox;
protected ImageView imageOfProduct;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.coposite_product_info, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.imageOfProduct = (ImageView) view.findViewById(R.id.productImageView);
viewHolder.productInfoText = (TextView) view.findViewById(R.id.productInfoText);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.productCheck);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Products element = (Products) viewHolder.checkbox
.getTag();
element.setChecked(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.productInfoText.setText(list.get(position).getInformation());
holder.imageOfProduct.setImageBitmap(list.get(position).getProductImage());
holder.checkbox.setChecked(list.get(position).isChecked());
return view;
}
I wanna get checked items from these but i couldn't reach them. When i debug it on my main class which is call listview.getCheckedItemPositions(); always turn for me "null"
This doesn't crash actually just always show "null" but i can see all my data in my listview.
SparseBooleanArray checkedItems = shopListView.getCheckedItemPositions();

Categories

Resources