0

I am trying to have a clickable listView of items...For each element in the list, I am having a clickable TextView....the problem is that it's too limited for the user to click only on the TextView to access what's inside the element. Instead, I want the whole list item that holds that TextView to be clickable so that the user can easily click anywhere (not specifically on the text). Can anyone help? Thank you.

import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends ListActivity implements OnCheckedChangeListener {

TextView label;
CheckBox checkBox;
Context c = this.getApplicationContext();
private ArrayList<Boolean> status = new ArrayList<Boolean>();

public class MyCustomAdapter extends ArrayAdapter<String> {


public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {

super(context, textViewResourceId, objects);

    for (int i = 0; i < objects.length; i++) {
        status.add(false);
    }
  // TODO Auto-generated constructor stub
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
 // TODO Auto-generated method stub
 //return super.getView(position, convertView, parent);

 View row = convertView;

 if(row==null){
  LayoutInflater inflater=getLayoutInflater();
  row=inflater.inflate(R.layout.main, parent, false); 

 }  


 label=(TextView)row.findViewById(R.id.weekofday);
 label.setText(month[position]);
 label.setClickable(true);

//View l = (View)row.findViewById(R.id.linearLayout1);
 label.setOnClickListener(new View.OnClickListener() {

 public void onClick(View v) {

     Toast.makeText(getApplicationContext(), "The rest just clicked in " + position ,       Toast.LENGTH_SHORT).show();  

 }   });



 checkBox=(CheckBox)row.findViewById(R.id.checkBox);
 checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

         String event = null;

         if (isChecked) 
         {
         status.set(position, true);
         event = " just checked!";
         } 

        else 
         {
         status.set(position, false);
         event = " just unchecked!";
         }

         Toast.makeText(getApplicationContext(), "checkbox " + position +event,      Toast.LENGTH_SHORT).show();   

     }
 });
 checkBox.setChecked(status.get(position));



 return row;
}
}

 String[] month = {
 "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
  };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
 /*setListAdapter(new ArrayAdapter<String>(this,
   R.layout.row, R.id.weekofday, DayOfWeek));*/


   setListAdapter(new MyCustomAdapter(ListViewActivity.this, R.layout.main, month));



 }













 }
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
  • take a look at this question, it might help you: http://stackoverflow.com/questions/2026873/android-way-to-appear-bordered-text-on-the-textview – MByD Apr 08 '12 at 18:52
  • So you are using `OnClickListener`? If so, you are doing it wrong. Set an `OnItemClickListener` to your `ListView` and that should do it. – Cristian Apr 08 '12 at 18:52
  • I don't have the normal ViewList, I am inheriting ArrayAdapter and overriding getView method to set each item seperately on my listView...I have two items. a dropbox and a Textview...I want the TextView to be clickable on the whole item. – Traveling Salesman Apr 08 '12 at 18:57
  • What exactly means you don't have a normal ListView? Could you post some cheezbur... source pls? Cause in the end you usually plug in a "source object" like an 'ArrayList' into the ListAdapter via 'setListAdapter() – nuala Apr 08 '12 at 19:17

1 Answers1

1

you can use

listview.setOnItemOnClickListener(new OnItemClickListener(....));
MAC
  • 15,799
  • 8
  • 54
  • 95