android - 我正在使用 Toast 在 ListViewItem 单击时显示,但没有显示 toast

标签 android android-listview adapter android-toast

我正在使用以下代码,我想在单击 ListView 项目后显示一些文本。但 Toast 不会出现在点击时。我已经使用 ArrayList 来存储列表项

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

//这里我使用了listview点击

 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;
    }

}

最佳答案

添加这个

android:descendantFocusability="blocksDescendants"

到xml中的根元素

当您单击列表项时,复选框会获得焦点。所以将以上内容添加到 xml 中的 RelativeLayout 并重新运行应用程序。

编辑:

仔细观察你已经有了 android:focusableInTouchMode="false" 所以上面的可能不起作用。

编辑 2:

我只用相同布局的字符串试过你的代码

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>

快照

enter image description here

关于android - 我正在使用 Toast 在 ListViewItem 单击时显示,但没有显示 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396718/

相关文章:

java - 在 android 数组适配器中使用 notifyDataSetChanged 时出错

android - fragment 中的 ArrayAdapter - Android

android - @Mention 使用 Multiautocompletetextview

Android layoutdisplay,layout over layout。

Android默认分组指示器ExpandableListView

java - Android - 列出Activity到Activity

android - 如何从适配器调用 ListFragment 中定义的方法?

java - 使用 Assets 时 Android WebView 内存泄漏

java - 如何重新启用屏幕调光?

Android:在 ImageView 中显示图像数组