1

我在列表视图中使用复选框,同时单击复选框它必须选择行中的内容。但没有占据当前位置。但不是第一次选择当前位置,第二次没有选择任何东西。没有选择当前位置,随机选择。

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item2);
        lv =(ListView)findViewById(R.id.list);
        mDbHelper = new GinfyDbAdapter(this);
        share = (Button)findViewById(R.id.btnget);
        btnadd1 = (Button)findViewById(R.id.btnadd);
        lv = getListView();
        share.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {

                StringBuilder result = new StringBuilder();
                for(int i=0;i<mCheckStates.size();i++)

                {
                    if(mCheckStates.get(i)==true)
                    {
                         result.append("Title:");
                         result.append(bb.get(i));
                         result.append("\n");
                         result.append("Content:");
                         result.append(aa.get(i));
                         result.append("\n");
                    }

                }
               // }
                 showAlertView(result.toString().trim());

            }

        });

        btnadd1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                createProject();


            }

        });

        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());


    }


    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    private void fillData() {
        mDbHelper.open();      
        Cursor projectsCursor = mDbHelper.fetchAllProjects();

        int count = projectsCursor.getCount();
        Log.i(".................",""+count);
        if (projectsCursor.moveToFirst()) {
              do {

                  int col1 = projectsCursor.getColumnIndex("title");
                  String title = projectsCursor.getString(col1 );         
                  bb.add(title);              

                      int col2 = projectsCursor.getColumnIndex("content");
                                String content = projectsCursor.getString(col2 );         
                            aa.add(content);
              } while (projectsCursor.moveToNext());

        }
        //startManagingCursor(projectsCursor);


        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE,GinfyDbAdapter.CATEGORY_COLUMN_CONTENT,GinfyDbAdapter.CATEGORY_COLUMN_DATE};

        int[] to = new int[]{R.id.text22,R.id.text11,R.id.date};
        dataAdapter = new CustomAdapter (YourPrayerActivity .this, R.layout.row2, projectsCursor, from, to);
        setListAdapter(dataAdapter);



        EditText myFilter = (EditText) findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                dataAdapter.getFilter().filter(s.toString());
            }
        });

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return mDbHelper.fetchProjectByName(constraint.toString());
            }
        });



        tts = new TextToSpeech(this, this);
        final ListView lv = getListView();
        txtText = (TextView) findViewById(R.id.text11);
        lv.setTextFilterEnabled(true);



    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                //btnaudioprayer.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }




    private void createProject() {
        Intent i = new Intent(this, AddyourprayerActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);  
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }


    private void speakOut() {

        // String text = txtText.getText().toString();
        // String text = "Android speech";

        tts.speak(typed, TextToSpeech.QUEUE_FLUSH, null);
    }

    class CustomAdapter extends SimpleCursorAdapter implements CompoundButton.OnCheckedChangeListener {

        private LayoutInflater mInflater;
        private ListView lv;




        @SuppressWarnings("deprecation")
        public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
                 {
            super(context, layout, c, from, to);
            mInflater= LayoutInflater.from(context);
            mCheckStates = new SparseBooleanArray(c.getCount());

        }


         @Override
            public void bindView(View view, Context context, final Cursor cursor){


             if (view != null) {
                int row_id = cursor.getColumnIndex("_id");  //Your row id (might need to replace)
                TextView tv = (TextView) view.findViewById(R.id.text22);
                final TextView tv1 = (TextView) view.findViewById(R.id.text11);
                TextView tv2 = (TextView) view.findViewById(R.id.date);
                CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
                int col1 = cursor.getColumnIndex("title");
                final String title = cursor.getString(col1 );
                int col2 = cursor.getColumnIndex("content");
                final String content = cursor.getString(col2 );
                int col3 = cursor.getColumnIndex("date");
                final String date = cursor.getString(col3);
                cb.setTag(cursor.getPosition());
                cb.setChecked(mCheckStates.get(cursor.getPosition()+1, false));
                cb.setOnCheckedChangeListener(this);

                tv.setText( title);
                tv1.setText( content);
                tv2.setText(date);

                ImageButton button = (ImageButton) view.findViewById(R.id.sms1);
                button.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v){
                        StringBuffer sb2 = new StringBuffer();
                    sb2.append("Title:");
                        sb2.append(Html.fromHtml(title));
                        sb2.append(",Content:");
                        sb2.append(Html.fromHtml(content));
                        sb2.append("\n");
                        String strContactList1 = (sb2.toString().trim());
                        sendsmsdata(strContactList1);
                    }
                });

                ImageButton button1 = (ImageButton) view.findViewById(R.id.mail1);
                button1.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v){
                        StringBuffer sb3 = new StringBuffer();
                    sb3.append("Title:");
                        sb3.append(Html.fromHtml(title));
                        sb3.append(",Content:");
                        sb3.append(Html.fromHtml(content));
                        sb3.append("\n");
                        String strContactList2 = (sb3.toString().trim());
                        sendmaildata(strContactList2);
                    }
                });

                ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer1);
                button2.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v){
                        //ADD STUFF HERE you know which row is clicked. and which button
                        typed = content;
                        speakOut();
                    }
                });
             }





            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent){
                LayoutInflater inflater = LayoutInflater.from(context);
                View v = inflater.inflate(R.layout.row2, parent, false);

                bindView(v,context,cursor);
                return v;
            }

            public boolean isChecked(int position) {
                return mCheckStates.get(position, false);
            }

            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);

            }

            public void toggle(int position) {
                setChecked(position, !isChecked(position));

            }
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
            // TODO Auto-generated method stub

             mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
}

在单击复选框时,然后必须单击共享按钮后,它将显示短信或电子邮件,如果我们单击短信,在该内容中,我们检查了哪些内容必须存在于 msg 内容中。

我在调试中检查,如果我选择第一行或第二行,则它会占用,否则如果我首先选择第三行,它不会占用内容。

4

4 回答 4

1

有一个计数变量作为类成员

int count;

然后在fillData

count = projectsCursor.getCount();

所以当你点击一个按钮

    StringBuilder result = new StringBuilder();
            if(count>0) // check if count is greater than o
                            // count can be 0 if you don't select any check box
            {
            for(int i=0;i<count;i++)

            {Log.i("checked content Inside on click of share ",""+aa.get(i));
                if(mCheckStates.get(i)==true)
                {
                     result.append("Title:");
                     result.append(bb.get(i));

                     result.append("\n");
                     result.append("Content:");
                     result.append(aa.get(i));
                     result.append("\n");

                }

            }
            }

您正在使用 SparseBoolean 数组,这对于您检查的行是正确的。然后根据选中的项目检索数据。

这是我从中挑选的样本

https://groups.google.com/forum/#!topic/android-developers/No0LrgJ6q2M

您所做的是您没有通过整个项目列表来检查复选框是否被选中。

        for(int i=0;i<mCheckStates.size();i++) // this was the problem

如果只检查两项,您将获得前两项。

于 2013-09-20T05:11:14.863 回答
0

为避免 ListView 中 CheckBoxes 的问题,您可以在开始时采用一个初始化为 false 的布尔数组,然后将在 ListView 中选中复选框的数组中的相应位置设为 true。请在此处参考此演示:

Android复选框多选问题

于 2013-09-19T11:06:32.517 回答
0

为什么不使用 arrayAdapter?

看看这个链接。希望它可以帮助你。

< http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html >

于 2013-09-19T12:22:05.530 回答
0

由于每次滚动时它都会重新创建视图的行为或 getview() 方法,因此必须使用 setTag 和 getTag 来保持位置,甚至将选中的检查存储到具有适当位置的数组中。

试试下面的例子,你可以用复选框替换切换按钮。

Listview 中的 Getview 方法怪异

于 2013-09-19T12:36:20.840 回答