2016-07-22 2 views
0

Я показываю отели в списке с флагом, изображением и текстом. когда пользователь нажмет на флажок, поставьте флажок hotel_id в пакет. И переходите к этому суперклассу.Как передать пользовательский объект с установленным флажком с помощью намерения в адаптере к действию

Когда я выбираю отели и нажимаю кнопку на основной деятельности, мой лог-код показывает NullPointerException. Я проверю свой код с помощью отладчика. Я могу найти получение Bundle = null. Поэтому, пожалуйста, помогите найти hotel_id, когда вы нажмете на флажок и отправьте, что все выбранные hotel_id к его основной деятельности.

Вот мой класс ListViewAdapter.

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

Вот мой 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); 
} 
} 

Мой класс пользовательского объекта здесь.

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

используя интерфейс вы можете сделай это. –

+0

Как я могу объяснить. Я не знаю, что я новичок в андроиде. если у вас есть решение для этого, тогда plz измените мой код, отправьте сообщение на него. Я буду googling последние два так PLZ. –

+0

проверить эту ссылку http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html –

ответ

0

Проблема в том, что вы только помещаете данные в намерения. Это просто стоит, если вы поместите пакет или данные внутри объекта цели с помощью функции startActivity.

Если вы хотите, чтобы получить выбранный идентификатор отеля просто сделать общественный массив, как: -

public class Hotels extends Activity 
{ 
public ArrayList<Integer> hotel_ids=new ArrayList<>(); 
} 

и внутри адаптера при выборе флажок поместить идентификатор в ArrayList с использованием контекста, как показано ниже, и если ее не остановить удалить hotel_id из этого списка массивов: -

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()) { 
         if(context instanceof Hotels){ 
      ((Hotels)context).hotel_ids.add(p.getId()); 
         } 
         checkCounter++; 

        } else { 
         if(context instanceof Hotels){ 
       ((Hotels)context).hotel_ids.remove(p.getId()); 
         } 
         checkCounter--; 
        } 

       } 
    } 
} 

Это может помочь вам. С вышеуказанным вы всегда найдете выбранные Hotel_ids.

+0

Можете ли вы отправить мне код выше с помощью намерения. –

+0

Что это значит, он показывает красную линию. if (контекстный экземпляр Hotels) –

+0

, если я использую контекст.startActivity (намерение) внутри setOnClickListener. Затем он будет перезапускать активность в отелях при каждом нажатии на флажок. и удалить статус флажка и отобразить существующий список как есть. Так я не могу использовать его в своем коде. могу ли я сказать, где я могу использовать context.startActivity (намерение) в моем коде. –

0

вы можете сделать что-то вроде этого.

1> в методе GetView вы можете сделать проверены слушателем, как показано ниже

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

2> сделать новый класс для CheckUpdatelistener с аргументом конструктора класса модели

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> в основной деятельности по вы можете сделать код следующим образом:

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

Не нужно выделение жирным шрифтом ... –

+0

Ошибка на этой строке viewHolder.chkbox.setOnCheckedChangeListener (новый CheckUpdateListener (родительский)); 'CheckUpdateListener (com.hotel.yasmeenshaikh.GoHolidays.Product)' имеет частный доступ в 'com.hotel.yasmeenshaikh.GoHolidays.CheckUpdateListener' –

+0

@yogipuranik sorry я не могу, вам просто нужно пройти класс модели там –

Смежные вопросы