lundi 14 décembre 2015

Both Options Should Work---> Select All Items Through One Checkbox and Select Few From The Listview Android

I am new to Android. I want to select all the items through one checkbox in the Main Class and select from the list both the options and send the info of selected items on button click. I am also facing a Strange problem when I am selecting items from the listview, others are selected automatically. How this happening Here I am Posting My Code. I Hope my problem will be solved here.

User_Info.java

ListView listview;
CheckBox checkbox;
Button btn_send;
ArrayList<Getter_Setter> user_list;
UserInfoAdapter adapter;
ProgressDialog dialog;
Parser parser = new Parser();
String url = "http://...............";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_info);

    listview = (ListView) findViewById(R.id.listview);
    checkbox = (CheckBox) findViewById(R.id.checkbox);
    btn_send = (Button) findViewById(R.id.btn_send);

    ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cn.getActiveNetworkInfo();
    if (nf != null && nf.isConnected() == true) {

        new User_Info().execute();
    }

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

        }
    });
}

public class User_Info extends AsyncTask <String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(UserInfo.this);
        dialog.setIndeterminate(false);
        dialog.setMessage("Please Wait....");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        try {

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            JSONObject json = parser.getJSONFromUrl(url,nameValuePairs);
            user_list = new ArrayList<Getter_Setter>();

            int result = json.getInt("udata");

            JSONArray array = json.getJSONArray("result");

            if (result == 1){
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    Getter_Setter getter_setter = new Getter_Setter();
                    getter_setter.setName(jsonObject.getString("name"));
                    getter_setter.setEmail(jsonObject.getString("email"));

                    user_list.add(getter_setter);
                    adapter = new UserInfoAdapter(UserInfo.this,user_list);
                }

            } if (result == 0){
                return "Something Went Wrong";
            }
         } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Exception", "" + e.toString());
            return "Try Again";
        }
        return null;
    }

    protected void onPostExecute(String result) {
        // dismiss the dialog once product deleted
        super.onPostExecute(result);
        dialog.dismiss();
        listview.setAdapter(adapter);
    }
}

activity_user_info.xml:

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/linear_header"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/app_header_logo"
        android:gravity="center" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:textAllCaps="false"
        android:textSize="15sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All" />

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.85"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>


</LinearLayout>

UserInfoAdapter:

public class UserInfoAdapter extends BaseAdapter {

Context context;
ArrayList<Getter_Setter> user_info_list;


public UserInfoAdapter(Context mContext, ArrayList<Getter_Setter> m_user_info_list) {

    this.context = mContext;
    this.user_info_list = m_user_info_list;
}

@Override
public int getCount() {
    return user_info_list.size();
}

@Override
public Object getItem(int position) {
    return user_info_list.get(position);
}

@Override
public long getItemId(int position) {
    return user_info_list.indexOf(position);
}

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

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.user_info_adapter, null);

        TextView Name = (TextView) convertView.findViewById(R.id.txt_name);
        TextView Email = (TextView) convertView.findViewById(R.id.txt_email);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        try {
            Name.setText(user_info_list.get(position).getName());
            Email.setText(user_info_list.get(position).getEmail());

        } catch (Exception e) {
            e.printStackTrace();
        }
        }
        return convertView;
    }
}

user_info_adapter.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightLarge"
android:gravity="center"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="7dp"
            android:text="Some Item"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#333333" />

        <TextView
            android:id="@+id/txt_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="7dp"
            android:layout_marginLeft="10dp"
            android:gravity="center|center_horizontal"
            android:text="email"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#2980b9"
            android:textStyle="bold" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true" />

 </RelativeLayout>


</LinearLayout>

Getter_Setter:

public class Getter_Setter {

private String Name;
private String Email;

public boolean isclicked() {
    return isclicked;
}

public void setIsclicked(boolean isclicked) {
    this.isclicked = isclicked;
}

public boolean isclicked;

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
 }


}




Aucun commentaire:

Enregistrer un commentaire