Question

I am using the following code where I want to display some text after ListView Item click. But Toast doesnt appear on click. I have used ArrayList to store the List items

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    linear_top_right = new LinearLayout(this);
    linear_top_right.setOrientation(LinearLayout.HORIZONTAL);

    linear_top_left = new LinearLayout(this);
    linear_top_left.setOrientation(LinearLayout.HORIZONTAL);

    linear_top = new LinearLayout(this);
    linear_top.setOrientation(LinearLayout.HORIZONTAL);

    linear_list = new LinearLayout(this);
    linear_list.setOrientation(LinearLayout.VERTICAL);

    linear_All = new LinearLayout(this);
    linear_All.setOrientation(LinearLayout.VERTICAL);

    btn_delete = new Button(this);
    btn_delete.setBackgroundResource(R.drawable.select_minus);
    btn_delete.setWidth(15);
    btn_delete.setHeight(15);

    btn_add = new Button(this);
    btn_add.setBackgroundResource(R.drawable.select_add);
    btn_add.setWidth(15);
    btn_add.setHeight(15);

    linear_top_right.addView(btn_delete);
    linear_top_right.addView(btn_add);


    chk_select_all = new CheckBox(this);
    chk_src_to_dest = new CheckBox(this);

    chk_select_all.setText("All");
    chk_src_to_dest.setText("SRC to Dest");

    linear_top_left.addView(chk_select_all);
    linear_top_left.addView(chk_src_to_dest);

    listView = new ListView(this);
    linear_list.addView(listView);

    dataAdapter = new MyCustomAdapter(this, R.layout.row_create_list,
            allWordList);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(dataAdapter);

    linear_top_right.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    linear_top_right.setGravity(Gravity.RIGHT);

    linear_top_left.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    linear_top.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    linear_list.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    RelativeLayout relative1 = new RelativeLayout(this);
    relative1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));

    linear_top.addView(linear_top_left);
    linear_top.addView(linear_top_right);
    linear_All.addView(linear_top);
    linear_All.addView(linear_list);
    relative1.addView(linear_All);
    setContentView(relative1);

    final Thread thread = new Thread(){
        @Override
       public void run() {
            try {
               Thread.sleep(5000); // As I am using LENGTH_LONG in Toast
               PracticeTest.this.finish();
           } catch (Exception e) {
               e.printStackTrace();
           }
        }  
      };

//Here I have used listview click

 listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(PracticeTest.this,"BETA VERSION",
                       Toast.LENGTH_LONG).show();
            //thread.start();
            WordList list = (WordList) parent.getItemAtPosition(position);
            String listId = list.ListId;
            String listName = list.ListName;
            Intent intent = new Intent(PracticeTest.this,
                    TestListActivity.class);
            intent.putExtra(Constant.LIST_ID, listId);
            intent.putExtra(Constant.LIST_NAME, listName);

            PracticeTest.this.startActivity(intent);
        }
    });
    chk_src_to_dest
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button,
                        boolean checked) {
                    FromSrcToDes = checked;
                }
            });

    chk_select_all
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button,
                        boolean checked) {
                    for (WordList list : allWordList) {
                        list.Ischecked = checked;
                    }
                    dataAdapter.notifyDataSetChanged();
                }
            });

    btn_delete.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            int itemCount = allWordList.size();
            for (int i = itemCount - 1; i >= 0; i--) // for(WordList list :
                                                        // allWordList)
            {
                WordList list = allWordList.get(i);
                if (list.Ischecked == true) {
                    Util.deleteListId(PracticeTest.this, list.ListId);
                }
            }

            loadAllLists(); // dataAdapter.notifyDataSetChanged(); //
            chk_select_all.setChecked(false);
        }
    });

    btn_add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(PracticeTest.this,
                    AddWordsToList.class);
            intent.putExtra(Constant.LIST_ID, "");
            PracticeTest.this.startActivity(intent);

        }
    });
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    if (chk_select_all != null)
        chk_select_all.setChecked(false);
    loadAllLists();
}

private void loadAllLists() {
    allWordList.clear();
    String[] listIds = Util.getListIds(this);
    for (int i = 0; i < listIds.length; i++) {
        String listid = listIds[i];
        if (!"".equals(listid)) {
            WordList list = null;
            list = Util.getListData(listid, this);
            if (list != null)
                allWordList.add(list);
        }
    }

    dataAdapter = new MyCustomAdapter(this, R.layout.row_create_list,
            allWordList);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(dataAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            // give test

            WordList list = (WordList) parent.getItemAtPosition(position);
            String listId = list.ListId;
            String listName = list.ListName;
            Intent intent = new Intent(PracticeTest.this,
                    TestListActivity.class);
            intent.putExtra(Constant.LIST_ID, listId);
            intent.putExtra(Constant.LIST_NAME, listName);
            PracticeTest.this.startActivity(intent);
        }
    });
}

private class MyCustomAdapter extends ArrayAdapter<WordList> {

    private ArrayList<WordList> list;

    public MyCustomAdapter(Context context, int textViewResourceId,
            ArrayList<WordList> list) {
        super(context, textViewResourceId, list);
        this.list = list;
    }

    private class ViewHolder {
        CheckBox checkBx;
        TextView txt_result;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.row_create_list, null);

            holder = new ViewHolder();
            holder.checkBx = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.txt_result = (TextView) convertView
                    .findViewById(R.id.txt_result);
            holder.checkBx
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton button,
                                boolean checked) {

                            list.get(position).Ischecked = checked;
                        }
                    });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkBx.setText(list.get(position).ListName);
        holder.checkBx.setChecked((list.get(position).Ischecked));
        if (list.get(position).TotalWordsAttampted > 0) {
            holder.txt_result.setText("Last attempt "
                    + list.get(position).RightAnswers + "/"
                    + list.get(position).TotalWordsAttampted);
        }
        return convertView;
    }

}
Was it helpful?

Solution

Add this

android:descendantFocusability="blocksDescendants"

To the root element in xml

When you click on list item the checkbox takes focus. So add the above to the RelativeLayout in xml and re-run the app.

Edit:

Looking closely you already have android:focusableInTouchMode="false" so the above might not work.

Edit 2:

I tried your code with just strings with same layout

public class MainActivity extends Activity
{
 LinearLayout linear_top_right,linear_top_left,linear_top,linear_All,linear_list;
 Button btn_delete,btn_add;
 CheckBox chk_select_all, chk_src_to_dest;
 ListView listView; 
 MyCustomAdapter dataAdapter;
 String allWordList[]= {"A","B","C"};
 @Override
 protected void onResume() {
     // TODO Auto-generated method stub
     super.onResume();

     if (chk_select_all != null)
         chk_select_all.setChecked(false);

 }
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    linear_top_right = new LinearLayout(this);
    linear_top_right.setOrientation(LinearLayout.HORIZONTAL);

    linear_top_left = new LinearLayout(this);
    linear_top_left.setOrientation(LinearLayout.HORIZONTAL);

    linear_top = new LinearLayout(this);
    linear_top.setOrientation(LinearLayout.HORIZONTAL);

    linear_list = new LinearLayout(this);
    linear_list.setOrientation(LinearLayout.VERTICAL);

    linear_All = new LinearLayout(this);
    linear_All.setOrientation(LinearLayout.VERTICAL);

    btn_delete = new Button(this);

    btn_delete.setWidth(15);
    btn_delete.setHeight(15);

    btn_add = new Button(this);

    btn_add.setWidth(15);
    btn_add.setHeight(15);

    linear_top_right.addView(btn_delete);
    linear_top_right.addView(btn_add);


    chk_select_all = new CheckBox(this);
    chk_src_to_dest = new CheckBox(this);

    chk_select_all.setText("All");
    chk_src_to_dest.setText("SRC to Dest");

    linear_top_left.addView(chk_select_all);
    linear_top_left.addView(chk_src_to_dest);

    listView = new ListView(this);
    linear_list.addView(listView);

    dataAdapter = new MyCustomAdapter(this, R.layout.m,
            allWordList);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(dataAdapter);

    linear_top_right.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    linear_top_right.setGravity(Gravity.RIGHT);

    linear_top_left.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    linear_top.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    linear_list.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    RelativeLayout relative1 = new RelativeLayout(this);
    relative1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));

    linear_top.addView(linear_top_left);
    linear_top.addView(linear_top_right);
    linear_All.addView(linear_top);
    linear_All.addView(linear_list);
    relative1.addView(linear_All);
    setContentView(relative1);

    final Thread thread = new Thread(){
        @Override
       public void run() {
            try {
               Thread.sleep(5000); // As I am using LENGTH_LONG in Toast
               MainActivity.this.finish();
           } catch (Exception e) {
               e.printStackTrace();
           }
        }  
      };
//Here I have used listview click


    chk_src_to_dest
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button,
                        boolean checked) {

                }
            });

    chk_select_all
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button,
                        boolean checked) {

                }
            });

    btn_delete.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


        }
    });

    btn_add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }
    });




    dataAdapter = new MyCustomAdapter(this, R.layout.m,
            allWordList);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(dataAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) {
               Toast.makeText(MainActivity.this,"BETA VERSION",
                          Toast.LENGTH_LONG).show();



           }
       });
}

private class MyCustomAdapter extends ArrayAdapter<String> {

    private String[] list;

    public MyCustomAdapter(Context context, int textViewResourceId,
            String[] allWordList) {
        super(context, textViewResourceId, allWordList);
        this.list = allWordList;
    }

    private class ViewHolder {
        CheckBox checkBx;
        TextView txt_result;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.m, null);

            holder = new ViewHolder();
            holder.checkBx = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.txt_result = (TextView) convertView
                    .findViewById(R.id.txt_result);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        return convertView;
    }

}

m.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="checkbox" />

    <TextView
        android:id="@+id/txt_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text=" " />

</RelativeLayout>

Snap

enter image description here

OTHER TIPS

change the context of the toast to:

Toast.makeText(getApplicationContext(),"BETA VERSION",
                   Toast.LENGTH_LONG).show();

This is happening due to the click listerners available in your ListView item (checkbox in this case).

You have to add the below to your Checkboxes (they will work as normal :)),

android:focusable="false"
android:focusableInTouchMode="false"

Similar solution here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top