如何将带有选中复选框的自定义对象通过适配器中的intent传递到活动 [英] How to pass custom object with checked checkbox through intent in adapter to activity

查看:202
本文介绍了如何将带有选中复选框的自定义对象通过适配器中的intent传递到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在listview中显示带有复选框,图片和文字的酒店。当用户点击复选框,然后把检查hotel_id到捆绑。

I am displaying hotels in listview with checkbox,image and text. when user click on checkbox then put that checked hotel_id into bundle. And pass to it's super class.

当我选择酒店并点击主活动按钮时,我的logcat显示NullPointerException。我将检查我的代码与调试器。我可以找到Bundle = null。所以,请帮助找到hotel_id,当点击复选框,并发送所有选择的hotel_id到其主要活动。

When i am selected hotels and click on button on main activity my logcat shows NullPointerException. I will check my code with debugger. I can find getting Bundle=null. So please help to find the hotel_id when click's on checkbox and send that all selected hotel_id to its main Activity.

这是我的ListViewAdapter类。

Here is my ListViewAdapter class.

public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<Product> AllMenu = new ArrayList<>();
ImageLoader imageLoader;
int checkCounter = 0;
public ListViewAdapter(Context context, ArrayList<Product> itemlist) {
    this.context = context;
    AllMenu = itemlist;
    imageLoader = new ImageLoader(context);
    checkCounter = 0;
}
public int getCount() {
    return AllMenu.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, final View convertView, final ViewGroup parent) {
    // Declare Variables
    final Product tempMenu = AllMenu.get(position);
    final CheckBox c;
    final ImageView image_path;
    final TextView name, location, desc;
    final Bundle b = new Bundle();
    final Intent intent = new Intent(context.getApplicationContext(),Hotels.class);

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View view = inflater.inflate(R.layout.viewpage, parent, false);

    // Get the position
    c = (CheckBox) view.findViewById(R.id.mycheckbox);
    name = (TextView) view.findViewById(R.id.fh_name);
    location = (TextView) view.findViewById(R.id.fh_loc);
    desc = (TextView) view.findViewById(R.id.fh_desc);
    image_path = (ImageView) view.findViewById(R.id.image_all_main);

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

            if (c.isChecked() && checkCounter >= 3) {
                AllMenu.get(position).setSelected(false);
                c.setChecked(false);
                Toast.makeText(context, "You can select max 3 hotels!!", Toast.LENGTH_SHORT).show();
            } else {
                Product p = (AllMenu).get(position);
                p.setSelected(c.isChecked());
                if (c.isChecked()) {
                    checkCounter++;

                } else {
                    checkCounter--;
                }

            }
            StringBuffer responseText = new StringBuffer();
            responseText.append("The following were selected...");

            ArrayList<Product> p = AllMenu;
            for (int i = 0; i < p.size(); i++) {
                Product pp = p.get(i);
                if (pp.isSelected()) {
    //here is i want hotel id when i am cliked on hotel.
    //and save it into bundle and i am get bundle into it's super class

                    String[] h = new String[0];
                    b.putStringArray(String.valueOf(pp.getId()),hid);
                    intent.putExtras(b);

    //Here is how i can pass custom object to string

                    responseText.append("\n" + pp.getName() + "\t");
                    responseText.append("\t" + pp.getLocation());
                }
            }
            Toast.makeText(context, responseText, Toast.LENGTH_SHORT).show();

        }
    });

    c.setTag(tempMenu);
    c.setChecked(tempMenu.isSelected());
    name.setText(tempMenu.getName());
    location.setText(tempMenu.getLocation());
    desc.setText(tempMenu.getDescription().trim());
    imageLoader.DisplayImage(tempMenu.getImage_path(), image_path);
   return view;
}
}

以下是我的Hotels.class

Here is my Hotels.class

public class  Hotels extends Activity
{
// Declare Variables
JSONArray jsonarray = null;
private static final String TAG_ID= "id";
public static final String TAG_NAME = "name";
public static final String TAG_LOCATION = "location";
public static final String TAG_DESC = "description";
String f_date,l_date;
int[] hotel_id = new int[0];


ProgressDialog loading;
ListView list;
Button booknow;
ListViewAdapter adapter;
private ArrayList<Product> itemlist;
Product product;
static String Array = "MyHotels";
View view;
CheckBox click;
String hotel = "http://app.goholidays.info/getHotelData.php";
String booking = "http://app.goholidays.info/insertIntoBooking.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_item_layout);
    product = new Product();

    itemlist = new ArrayList<Product>();
    new ReadJSON().execute();
    click = (CheckBox) findViewById(R.id.mycheckbox);
    booknow = (Button) findViewById(R.id.bookhotel);
    product = new Product();
    list = (ListView) findViewById(R.id.myimagelist);

    Calendar c = Calendar.getInstance();
    System.out.println("Current time => "+c.getTime());
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Hotels.this);
    SharedPreferences.Editor editor = sp.edit();

    final String user_id = sp.getString("id", TAG_ID);
    final String start_date = sp.getString("start_date", f_date);
    final String end_date = sp.getString("end_date", l_date );
    final String chk_status = df.format(c.getTime());

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

    //here i want get all selected hotel_id into bundle. 
    //And i want pass that hotel_id to my database.
            Bundle bundle = getIntent().getExtras();
            String[] hotel_id = bundle.getStringArray(String.valueOf(product.getId()));
            Submit(start_date,end_date,hotel_id,chk_status,user_id);
        }
    });
}

class ReadJSON extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loading = ProgressDialog.show(Hotels.this,"Fetching Data","Please wait...",false,false);
    }
    @Override
    protected String doInBackground(String... args) {
        Product tempMenu;
        try {
            JSONObject jsonobject = JSONfunctions.getJSONfromURL(hotel);
            jsonarray = jsonobject.optJSONArray(Array);
            //parse date for dateList
            for (int i = 0; i < jsonarray.length(); i++) {
                tempMenu = new Product();
                jsonobject = jsonarray.getJSONObject(i);
                tempMenu.setId(jsonobject.getInt("hotel_id"));
                tempMenu.setName(jsonobject.getString("name"));
                tempMenu.setLocation(jsonobject.getString("location"));
                tempMenu.setImage_path(jsonobject.getString("image_name"));
                tempMenu.setDescription(jsonobject.getString("description"));
                tempMenu.setFacility1(jsonobject.getString("facility1"));
                tempMenu.setFacility2(jsonobject.getString("facility2"));
                tempMenu.setFacility3(jsonobject.getString("facility3"));
                tempMenu.setFacility4(jsonobject.getString("facility4"));
                tempMenu.setStar(jsonobject.getString("star"));
                itemlist.add(tempMenu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        adapter = new ListViewAdapter(Hotels.this, itemlist);
        list.setAdapter(adapter);
        loading.dismiss();
    }
}
private void Submit(final String start_date, final String end_date, final String[] hotel_id, final String chk_status, final String user_id) {
    class BackTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected String doInBackground(String... params) {
            String start_dtae = params[0];
            String end_date = params[1];
            String hotel_id = params[2];
            String user_id = params[3];
            String chk_status = params[4];

            try {
                URL url = new URL(booking);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode("start_date", "UTF-8") +"="+URLEncoder.encode(start_dtae, "UTF-8") + "&" +
                        URLEncoder.encode("end_date", "UTF-8") +"="+URLEncoder.encode(end_date, "UTF-8") + "&" +
                        URLEncoder.encode("hotel_id", "UTF-8") +"="+URLEncoder.encode(hotel_id, "UTF-8") + "&" +
                        URLEncoder.encode("user_id", "UTF-8") +"="+URLEncoder.encode(user_id, "UTF-8") + "&" +
                        URLEncoder.encode("chk_status", "UTF-8") +"="+URLEncoder.encode(chk_status, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();

                InputStream is = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                String responce = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    responce += line;
                }
                bufferedReader.close();
                is.close();
                httpURLConnection.disconnect();
                return responce;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(),"Book Success"+result,Toast.LENGTH_SHORT).show();
        }
    }
    BackTask lg=new BackTask();
    lg.execute(start_date, end_date, chk_status, String.valueOf(hotel_id), user_id);
}
}

我的自定义对象类在这里。

My custom object class is here.

public class Product extends HashMap<String, String> {

private String name;
private int hotel_id;
private String description;
private String location;
private String image_path;
boolean selected;

public boolean isSelected(){
    return selected;
}
public void setSelected(boolean selected){
    this.selected = selected;
}

public void setId (int hotel_id) { this.hotel_id = hotel_id;}

public int getId() {
    return hotel_id;
}

public void setName (String name) { this.name = name;}

public String getName() {
    return name;
}

public void setLocation(String location) {
    this.location = location;
}

public String getLocation() {
    return location;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDescription() {
    return description;
}

public void setImage_path(String image_path) {
    this.image_path = image_path;
}

public String getImage_path() {
    return image_path;
}
}


推荐答案

you can do something like this.

1>在getview方法中,您可以像下面这样检查侦听器

1> in getview method you can do checked listener like below

  viewHolder.chkbox.setOnCheckedChangeListener(new CheckUpdateListener(itemList.get(position));

2>使用模型类的参数构造函数为CheckUpdatelistener创建新类

2> make new class for CheckUpdatelistener with argument constructor of model class

    private final class CheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
    private final Product product;

    private CheckUpdateListener(Product product) {
        this.product = product;


    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i("onCheckedChanged", "isChecked: " + isChecked);
        product.setChecked(isChecked);


        notifyDataSetChanged();




    }
}

3>在按钮点击监听器的主活动中,您可以这样做代码

3> in main activity on button click listener you can do code like this

     Arraylist<Product> selectedProduct = new ArrayList<>();




            if (itemList.size() > 0) {
                for (int i = 0; i < itemList.size(); i++) {
                    if (itemList.get(i).isChecked()) {
                        try {

                            selectedProduct.add(itemList.get(i));
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }
                    }
                }

这篇关于如何将带有选中复选框的自定义对象通过适配器中的intent传递到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆