mardi 28 avril 2015

Android checkbox list selection issue

i have a list and using adapter i am adding items to it.When i select first item of check box automatically first item after scroll gets selected and so the items of subsequent scrolls.what is the issue how to solve.

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.messagecleaner.MainActivity" >
<LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select the addresses don't want to see" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/idAddressList" 
        >        
    </ListView>
</LinearLayout>
</RelativeLayout>



enter code here

package com.example.messagecleaner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.text.AndroidCharacter;
import android.content.Context;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;



public class MainActivity extends ActionBarActivity {
    ListView lstAddress;
    Context mContext;
    List<String> arrAddress;

    class MyAddressAdapter extends ArrayAdapter<String>
    {
        List<String> address;
        public MyAddressAdapter(Context context, int resource,
                List<String> objects) {
            super(context, resource, objects);
            address=objects;
            System.out.println("---->"+address.size());
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
        //  return super.getView(position, convertView, parent);
            View row=convertView;
            if(row==null)
            {
                LayoutInflater inflator= getLayoutInflater();
                row = inflator.inflate(R.layout.addresscheckboxlistitem, parent, false);
                CheckBox ctv=(CheckBox) row.findViewById(R.id.idAddressCheckTextView);
                ctv.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        System.out.println("Position "+ isChecked);
                    }
                });
                //ctv.setText(arrAddress[position]);
                System.out.println(address.get(position));
                ctv.setText(address.get(position)); 


            return row;
            }
            else
            {

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

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

                        }
                    });
                    System.out.println(address.get(position));
                    ctv.setText(address.get(position));
                    return row;

            }
        }       

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lstAddress=(ListView) findViewById(R.id.idAddressList);
        mContext=this;
        arrAddress= new ArrayList<String>();
        Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
        //arrAddress=new String[cursor.getCount()];
        int i=0;
        if (cursor.moveToFirst()) { // must check the result to prevent exception
            do {
               String msgData = "";
               for(int idx=0;idx<cursor.getColumnCount();idx++)
               {

                   if(cursor.getColumnName(idx).equals("address"))
                   {
                       String msgAddress=cursor.getString(idx);
                       //msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
                   //    System.out.println(msgAddress);
                       //arrAddress[i]=msgAddress;
                       if(arrAddress.contains(msgAddress))
                       {

                       }
                       else
                       {
                           arrAddress.add(msgAddress);


                       }
                   }
               }
               // use msgData
               i++;
            } while (cursor.moveToNext());
            System.out.println("Address array "+arrAddress.size());
            lstAddress.setAdapter(new MyAddressAdapter(mContext, android.R.layout.simple_list_item_1, arrAddress));
        } else {
           // empty box, no SMS
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




Aucun commentaire:

Enregistrer un commentaire