Stefan
Stefan

Reputation: 193

TextView with AlerDialog

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

Upvotes: 0

Views: 192

Answers (2)

Noureddine AMRI
Noureddine AMRI

Reputation: 2942

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();

Upvotes: 1

elpolta
elpolta

Reputation: 66

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;

}

Upvotes: 1

Related Questions