问题
I am creating app like e-commerce with listview that containing images, textbox and one checkbox. Now my question is that, i want send cheched item with their hole to serve on onClickListener.Before send it to server, I will pass it to next activity and this activity contains one application form where user can fill form after completion filling form when user click on button all user info and checked item data send to server database. I don't know how to pass checked item data to server. And one more main thing is that my listview data is retrieve from server using json and php. so please send me code or links that i can use for to do this things. Thank You
Here is my code of lisview
public class Hotels extends Activity
{
// Declare Variables
JSONArray jsonarray = null;
SQLiteDatabase sqLite;
public static final String TAG_NAME = "name";
public static final String TAG_LOCATION = "location";
public static final String TAG_DESC = "description";
String name,arrival_date,departure_date,image,location,description,adult,kids;
ProgressDialog loading;
ListView list;
Button booknow;
ListViewAdapter adapter;
private ArrayList<Product> itemlist;
Product product;
static String Array = "MyHotels";
View view;
CheckBox click;
ArrayList<String> checked = new ArrayList<String>();
String hotel = "http://app.goholidays.info/getHotelData.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_item_layout);
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);
booknow.setOnClickListener(new MyPersonalClickListener("book_hotel",product,getApplicationContext()));
}
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.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 class MyPersonalClickListener implements View.OnClickListener {
String book_hotel;
Product name;
Context context;
public MyPersonalClickListener(String book_hotel, Product product, Context context) {
this.book_hotel = book_hotel;
this.name = product;
this.context = context;
}
@Override
public void onClick(View v){
if (click.isChecked()) {
startActivity(new Intent(Hotels.this, BookHotel.class));
}
else
{
Toast.makeText(context, "Please Select Hotel", Toast.LENGTH_SHORT).show();
}
}
}
}
Adapter class of listview
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 null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
// Declare Variables
final Product tempMenu = AllMenu.get(position);
final CheckBox c;
final ImageView image_path,facility1,facility_1;
ImageView facility2,facility_2;
ImageView facility3,facility_3;
ImageView facility4,facility_4;
ImageView star1,star2,star3,star4,star5;
TextView name,location,desc;
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);
facility1 = (ImageView) view.findViewById(R.id.fh_fc1);
facility_1 = (ImageView) view.findViewById(R.id.fh_fc11);
facility2 = (ImageView) view.findViewById(R.id.fh_fc2);
facility_2 = (ImageView) view.findViewById(R.id.fh_fc22);
facility3 = (ImageView) view.findViewById(R.id.fh_fc3);
facility_3 = (ImageView) view.findViewById(R.id.fh_fc33);
facility4 = (ImageView) view.findViewById(R.id.fh_fc4);
facility_4 = (ImageView) view.findViewById(R.id.fh_fc44);
star1 = (ImageView) view.findViewById(R.id.fh_s1);
star2 = (ImageView) view.findViewById(R.id.fh_s2);
star3 = (ImageView) view.findViewById(R.id.fh_s3);
star4 = (ImageView) view.findViewById(R.id.fh_s4);
star5 = (ImageView) view.findViewById(R.id.fh_s5);
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()){
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());
imageLoader.DisplayImage(tempMenu.getImage_path(),image_path);
if(tempMenu.getFacility1().equals("Pool")) {
facility1.setVisibility(view.VISIBLE);
facility_1.setVisibility(view.INVISIBLE);
}else {
facility_1.setVisibility(view.VISIBLE);
facility1.setVisibility(view.INVISIBLE);
}
if(tempMenu.getFacility2().equals("Bar")) {
facility2.setVisibility(view.VISIBLE);
facility_2.setVisibility(view.INVISIBLE);
}else {
facility_2.setVisibility(view.VISIBLE);
facility2.setVisibility(view.INVISIBLE);
}
if(tempMenu.getFacility3().equals("Gym")) {
facility3.setVisibility(view.VISIBLE);
facility_3.setVisibility(view.INVISIBLE);
}else {
facility_3.setVisibility(view.VISIBLE);
facility3.setVisibility(view.INVISIBLE);
}
if(tempMenu.getFacility4().equals("Theater")) {
facility4.setVisibility(view.VISIBLE);
facility_4.setVisibility(view.INVISIBLE);
}else {
facility_4.setVisibility(view.VISIBLE);
facility4.setVisibility(view.INVISIBLE);
}
if(tempMenu.getStar().equals("1")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.INVISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
}else if(tempMenu.getStar().equals("2")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
}else if(tempMenu.getStar().equals("3")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
}else if(tempMenu.getStar().equals("4")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.VISIBLE);
star5.setVisibility(view.INVISIBLE);
}else if(tempMenu.getStar().equals("5")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.VISIBLE);
star5.setVisibility(view.VISIBLE);
}
else {
star1.setVisibility(view.INVISIBLE);
star2.setVisibility(view.INVISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
}
return view;
}
}
After selecting item from list it will come on this activity.
public class BookHotel extends AppCompatActivity {
EditText arrival,departure;
Calendar myCalendar;
Button booknow;
final String[] qtyValues = {"0","1","2","3","4"};
Spinner adult,children;
String chkin,chkout,persons,kids;
CheckBox checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_hotel);
booknow = (Button) findViewById(R.id.bookmyhotel);
arrival = (EditText) findViewById(R.id.arrival_date);
departure = (EditText) findViewById(R.id.departure_date);
myCalendar = Calendar.getInstance();
adult = (Spinner) findViewById(R.id.adults);
children = (Spinner) findViewById(R.id.childrens);
booknow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(arrival.getText().length() <= 0){
arrival.setError("Please fill the field.!!");
}else if(departure.getText().length() <= 0){
departure.setError("Please fill the field.!!");
}else {
chkin = arrival.getText().toString();
chkout = departure.getText().toString();
persons = adult.getSelectedItem().toString();
kids = children.getSelectedItem().toString();
//Submit(chkin, chkout, persons, kids);
}
}
});
ArrayAdapter<String> aa=new ArrayAdapter<String>(getApplicationContext(),R.layout.qty_spinner_item,qtyValues);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adult.setAdapter(aa);
children.setAdapter(aa);
final DatePickerDialog.OnDateSetListener adate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateArrival();
}
};
final DatePickerDialog.OnDateSetListener ddate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDeparture();
}
};
arrival.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(BookHotel.this, adate, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
departure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(BookHotel.this, ddate, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateArrival() {
String myFormat = " MM/dd/yy "; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
arrival.setText(sdf.format(myCalendar.getTime()));
//departure.setText(sdf.format(myCalendar.getTime()));
}
private void updateDeparture() {
String myFormat = " MM/dd/yy "; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
//arrival.setText(sdf.format(myCalendar.getTime()));
departure.setText(sdf.format(myCalendar.getTime()));
}
}
I don't know how i can pass those selecting item and this info to server on onClickListener please help me solve this problem. Thank You And here is another problem i will posted on it is this
Why people are giving negative mark to it. :( If u don't like then ignore it please. :( . I know my english is very bad. but please don't do like this i m really facing this problem. I will search all over but nothing i will find thier like this.
回答1:
Finally i done this. Here is the my answer.
public class Hotels extends AppCompatActivity {
// Declare Variables
JSONParser jsonParser = new JSONParser();
JSONArray jsonarray = null;
private static final String TAG_SUCCESS = "success";
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;
ArrayList<String> ne = new ArrayList<String>();
ProgressDialog loading;
ListView list;
Button booknow;
private ArrayList<Product> itemlist;
Product product;
static String Array = "MyHotels";
View view;
CheckBox click;
String user_id,start_date,end_date,chk_status;
String[] hotel_id;
String hotel = "http://app.goholidays.info/getHotelData.php";
String booking = "http://app.goholidays.info/insertIntoBooking.php";
SharedPreferences sp;
SharedPreferences.Editor editor;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_item_layout);
product = new Product();
itemlist = new ArrayList<Product>();
click = (CheckBox) findViewById(R.id.mycheckbox);
booknow = (Button) findViewById(R.id.bookhotel);
product = new Product();
list = (ListView) findViewById(R.id.myimagelist);
//get current date and time
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
sp = PreferenceManager.getDefaultSharedPreferences(Hotels.this);
editor = sp.edit();
//get all variables into string
user_id = sp.getString("id", TAG_ID);
start_date = sp.getString("start_date", f_date);
end_date = sp.getString("end_date", l_date);
chk_status = df.format(c.getTime());
booknow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hotel_id = new String[ne.size()];
hotel_id = ne.toArray(hotel_id);
for(int i = 0; i < hotel_id.length ; i++){
Log.d("string is",(String)hotel_id[i]);
}
if (hotel_id.length == 0) {
Toast.makeText(Hotels.this, "Please select Hotel..!", Toast.LENGTH_SHORT).show();
}else {
new BackTask().execute();
}
}
});
}
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, facility1, facility_1;
ImageView facility2, facility_2;
ImageView facility3, facility_3;
ImageView facility4, facility_4;
ImageView star1, star2, star3, star4, star5;
final TextView name, location, desc;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
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);
facility1 = (ImageView) view.findViewById(R.id.fh_fc1);
facility_1 = (ImageView) view.findViewById(R.id.fh_fc11);
facility2 = (ImageView) view.findViewById(R.id.fh_fc2);
facility_2 = (ImageView) view.findViewById(R.id.fh_fc22);
facility3 = (ImageView) view.findViewById(R.id.fh_fc3);
facility_3 = (ImageView) view.findViewById(R.id.fh_fc33);
facility4 = (ImageView) view.findViewById(R.id.fh_fc4);
facility_4 = (ImageView) view.findViewById(R.id.fh_fc44);
star1 = (ImageView) view.findViewById(R.id.fh_s1);
star2 = (ImageView) view.findViewById(R.id.fh_s2);
star3 = (ImageView) view.findViewById(R.id.fh_s3);
star4 = (ImageView) view.findViewById(R.id.fh_s4);
star5 = (ImageView) view.findViewById(R.id.fh_s5);
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()) {
ne.add(AllMenu.get(position).getId());
checkCounter++;
} else {
ne.remove(AllMenu.get(position).getId());
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()) {
responseText.append("\n" + pp.getName() + "\t");
responseText.append("\t" + pp.getLocation());
}
}
Toast.makeText(context, responseText, Toast.LENGTH_SHORT).show();
}
});
c.setTag(tempMenu.get(position));
c.setChecked(tempMenu.isSelected());
name.setText(tempMenu.getName()+",");
location.setText(tempMenu.getLocation().trim());
desc.setText(tempMenu.getDescription().trim());
imageLoader.DisplayImage(tempMenu.getImage_path(), image_path);
if (tempMenu.getFacility1().equals("Pool")) {
facility1.setVisibility(view.VISIBLE);
facility_1.setVisibility(view.INVISIBLE);
} else {
facility_1.setVisibility(view.VISIBLE);
facility1.setVisibility(view.INVISIBLE);
}
if (tempMenu.getFacility2().equals("Bar")) {
facility2.setVisibility(view.VISIBLE);
facility_2.setVisibility(view.INVISIBLE);
} else {
facility_2.setVisibility(view.VISIBLE);
facility2.setVisibility(view.INVISIBLE);
}
if (tempMenu.getFacility3().equals("Gym")) {
facility3.setVisibility(view.VISIBLE);
facility_3.setVisibility(view.INVISIBLE);
} else {
facility_3.setVisibility(view.VISIBLE);
facility3.setVisibility(view.INVISIBLE);
}
if (tempMenu.getFacility4().equals("Theater")) {
facility4.setVisibility(view.VISIBLE);
facility_4.setVisibility(view.INVISIBLE);
} else {
facility_4.setVisibility(view.VISIBLE);
facility4.setVisibility(view.INVISIBLE);
}
if (tempMenu.getStar().equals("1")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.INVISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
} else if (tempMenu.getStar().equals("2")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
} else if (tempMenu.getStar().equals("3")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
} else if (tempMenu.getStar().equals("4")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.VISIBLE);
star5.setVisibility(view.INVISIBLE);
} else if (tempMenu.getStar().equals("5")) {
star1.setVisibility(view.VISIBLE);
star2.setVisibility(view.VISIBLE);
star3.setVisibility(view.VISIBLE);
star4.setVisibility(view.VISIBLE);
star5.setVisibility(view.VISIBLE);
} else {
star1.setVisibility(view.INVISIBLE);
star2.setVisibility(view.INVISIBLE);
star3.setVisibility(view.INVISIBLE);
star4.setVisibility(view.INVISIBLE);
star5.setVisibility(view.INVISIBLE);
}
return view;
}
}
class BackTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("start_date", start_date));
param.add(new BasicNameValuePair("end_date", end_date));
param.add(new BasicNameValuePair("chk_status", chk_status));
for (String value : hotel_id) {
param.add(new BasicNameValuePair("hotel_id[]", value));
}
param.add(new BasicNameValuePair("user_id", user_id));
JSONObject json = jsonParser.makeHttpRequest(booking, "POST", param);
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully book a hotels
Intent intent = new Intent(Hotels.this, HomePage.class);
startActivity(intent);
// closing this screen
finish();
} else {
Log.d("failed to book hotel", json.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
}
来源:https://stackoverflow.com/questions/38265590/listview-with-checkbox-and-send-checked-data-to-server-in-android