Android การเก็บค่า คลิ๊ก Listview ส่งค่าทีละ 1 ไปยัง database mysql
แค่ส่ง GET ไปที่ URL เป็นอะไรที่ง่ายมากๆ ครับ
Code (Android-Java)
Ion.with(this)
.load("https://www.thaicreate.com/android/getString.php")
.asString()
.withResponse()
.setCallback(new FutureCallback<Response<String>>() {
@Override
public void onCompleted(Exception e, Response<String> result) {
// print the String that was downloaded
System.out.println(result.getResult());
txtResult.setText(result.getResult());
}
});
Android and Ion (HTTP LIB:Library)
Date :
2017-02-14 10:14:53
By :
mr.win
ส่งในหน้าไหนครับ
Date :
2017-02-15 03:44:33
By :
atikan5566
ส่งที่ Event ตอนที่ Click ListView ก้ได้ครับ
Date :
2017-02-15 09:57:44
By :
mr.win
ส่งค่าตอนคลิกที่ละ 1 นี้คือ เก็บคนที่คลิกป่าวครับ แต่ถ้าเป็นแบบนั้น เราก็เพิ่มค่าที่ละ 1 โดย i=i+1 ประมาณนี้ แล้วเรา set event จากเม้นบ้างบนครับ ว่า ถ้า user คลิกในปุ่ม listview หน้านั้น แล้ว สั่ง POST หรือ GET ไป เพื่อ update ไปยังฐานข้อมูลครับ ประมาณนี้
Date :
2017-02-15 11:06:14
By :
heloman
MainActivity.java
Code (Android-Java)
package com.dedykuncoro.uthaitour;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.dedykuncoro.uthaitour.adapter.CustomListAdapter;
import com.dedykuncoro.uthaitour.app.AppController;
import com.dedykuncoro.uthaitour.module.Jarak;
import com.miguelcatalan.materialsearchview.MaterialSearchView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Kuncoro on 03/29/2016.
*/
public class MainActivity extends AppCompatActivity implements LocationListener,
SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipe;
ListView list;
CustomListAdapter adapter;
List<Jarak> itemList = new ArrayList<>();
Double latitude, longitude;
Criteria criteria;
Location location;
LocationManager locationManager;
String provider;
// sesuaikan ip laptop/PC atau menggunakan ip emulator bawaan android studio 10.0.2.2
private static final String url = "http://trcns1.esy.es/json_travel.php?userLat=";
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list_travel);
//button back
Button btnbacktrave = (Button) findViewById(R.id.btnbacktravel);
btnbacktrave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//btnhome
Button btnhometravel = (Button) findViewById(R.id.btnsearchtravel);
btnhometravel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Searchview.class);
startActivity(i); finish();
}
});
list = (ListView) findViewById(R.id.list1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, DetailTravel.class);
intent.putExtra("userName", itemList.get(i).getName());
intent.putExtra("userPic", itemList.get(i).getGambar());
intent.putExtra("userTim", itemList.get(i).getTim());
intent.putExtra("userDetail", itemList.get(i).getDetail());
intent.putExtra("userTell", itemList.get(i).getTell());
intent.putExtra("userLat", itemList.get(i).getLat());
intent.putExtra("userLng", itemList.get(i).getLng());
intent.putExtra("userRating", itemList.get(i).getRating());
startActivity(intent);
Log.d("userRating", itemList.get(i=i+1).getRating());
}
});
// menyamakan variabel pada layout dan java
list = (ListView) findViewById(R.id.list1);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe1);
// mengisi data dari adapter ke listview
adapter = new CustomListAdapter(this, itemList);
list.setAdapter(adapter);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
swipe.setOnRefreshListener(this);
swipe.post(new Runnable() {
@Override
public void run() {
swipe.setRefreshing(true);
travel();
}
}
);
}
@Override
public void onRefresh() {
travel();
}
// fungsi ngecek lokasi GPS device pengguna
private void travel() {
location = locationManager.getLastKnownLocation(provider);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// permintaan update lokasi device dalam waktu 10 detik
locationManager.requestLocationUpdates(provider, 10000, 1, this);
if (location != null) {
onLocationChanged(location);
callListVolley(latitude, longitude);
} else {
Toast.makeText(getBaseContext(), "กรุณาเปิด Gps",
Toast.LENGTH_LONG).show();
/* latitude longitude Alun-alun Demak sebagai default jika tidak ditemukan lokasi dari device pengguna */
callListVolley(15.3840902, 100.0253592);
}
}
// untuk menampilkan lokasi wisata terdekat dari device pengguna
private void callListVolley(double userLat, double userLng) {
itemList.clear();
adapter.notifyDataSetChanged();
swipe.setRefreshing(true);
JsonArrayRequest jArr = new JsonArrayRequest(url + userLat + "&userLng=" + userLng,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Jarak j = new Jarak();
j.setName(obj.getString("userName"));
j.setTim(obj.getString("userTim"));
j.setTell(obj.getString("userTell"));
j.setGambar(obj.getString("userPic"));
j.setDetail(obj.getString("userDetail"));
j.setLat(obj.getString("userLat"));
j.setLng(obj.getString("userLng"));
j.setRating(obj.getString("userRating"));
double jarak = Double.parseDouble(obj.getString("jarak"));
j.setJarak("" + round(jarak, 2));
itemList.add(j);
} catch (JSONException e) {
e.printStackTrace();
}
}
// memberitahu adapter jika ada perubahan data
adapter.notifyDataSetChanged();
swipe.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getBaseContext(), error.getMessage(), Toast.LENGTH_LONG).show();
swipe.setRefreshing(true);
}
});
// menambah permintaan ke queue
AppController.getInstance().addToRequestQueue(jArr);
}
@Override
public void onBackPressed() {
finish();
System.exit(0);
}
// untuk menyederhanakan angka dibelakan koma jarak
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
// untuk menentukan lokasi gps dari device pengguna
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// untuk melihat latitude longitude posisi device pengguna pada logcat ditemukan atau tidak
Log.d(TAG, " " + latitude + ", " + longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
ประวัติการแก้ไข 2017-02-15 20:57:59 2017-02-15 21:00:57
Date :
2017-02-15 20:56:37
By :
atikan5566
DetailTravel.java
Code (Android-Java)
package com.dedykuncoro.uthaitour;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.Rating;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
public class DetailTravel extends AppCompatActivity {
private Button button, btncalltravel, btngpsmaptravel;
private ImageView ShowImageTravel;
private TextView ShowNameTravel, ShowTimTravel, ShowDetailTravel, ShowRatingTravel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_travel);
//btnback
Button btnbacktrave = (Button) findViewById(R.id.btndetailbacktravel);
btnbacktrave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//btnhome
Button btnhometravel = (Button) findViewById(R.id.btndetailhometravel);
btnhometravel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), MainManu.class);
startActivity(i); finish();
}
});
ShowNameTravel = (TextView) findViewById(R.id.ShowNameTravel);
ShowNameTravel.setSelected(true);
ShowImageTravel = (ImageView) findViewById(R.id.ShowImageTravel);
ShowTimTravel = (TextView) findViewById(R.id.ShowTimTravel);
ShowDetailTravel = (TextView) findViewById(R.id.ShowDetailTravel);
ShowRatingTravel = (TextView) findViewById(R.id.ShowRatingTravel);
btncalltravel = (Button) findViewById(R.id.btncalltravel);
btngpsmaptravel = (Button) findViewById(R.id.btngpsmaptravel);
String username = getIntent().getStringExtra("userName");
String userpic = getIntent().getStringExtra("userPic");
String usertim = getIntent().getStringExtra("userTim");
String userdetail = getIntent().getStringExtra("userDetail");
String userrating = getIntent().getStringExtra("userRating");
final String usertell = getIntent().getStringExtra("userTell");
final String userlat = getIntent().getStringExtra("userLat");
final String userlng = getIntent().getStringExtra("userLng");
//show data detail
ShowNameTravel.setText(username);
Glide.with(this).load(userpic).into(ShowImageTravel);
ShowTimTravel.setText(usertim);
ShowDetailTravel.setText(userdetail);
ShowRatingTravel.setText(userrating);
btncalltravel.setText(usertell);
//Log.d("name", "username=" + username);
Log.d("pic", "userpic=" + userpic);
Log.d("tell", "usertell=" + usertell);
Log.d("lat", "userlat=" + userlat + userlng);
//ShowImage.setImageResource(ShowImage);
//button call
button = (Button) findViewById(R.id.btncalltravel);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + usertell));
if (ActivityCompat.checkSelfPermission(DetailTravel.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startActivity(callIntent);
}
});
//button map
Button buttonIntent = (Button) findViewById(R.id.btngpsmaptravel);
buttonIntent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("google.navigation:q=" + userlat + "," + userlng);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
startActivity(Intent.createChooser(intent
, "View map with"));
}
});
//Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia");
}
}
Date :
2017-02-15 21:02:50
By :
atikan5566
CustomAdapter
Code (Android-Java)
package com.dedykuncoro.uthaitour.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.dedykuncoro.uthaitour.MainActivity;
import com.dedykuncoro.uthaitour.R;
import com.dedykuncoro.uthaitour.app.AppController;
import com.dedykuncoro.uthaitour.module.Jarak;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Created by Kuncoro on 03/29/2016.
*/
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Jarak> jarakItems;
ImageLoader imageLoader;
public CustomListAdapter(Activity activity, List<Jarak> jarakItems) {
this.activity = activity;
this.jarakItems = jarakItems;
}
@Override
public int getCount() {
return jarakItems.size();
}
@Override
public Object getItem(int location) {
return jarakItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.userPic);
TextView userName = (TextView) convertView.findViewById(R.id.userName);
userName.setSelected(true);
TextView userTim = (TextView) convertView.findViewById(R.id.userTim);
TextView userTell = (TextView) convertView.findViewById(R.id.userTell);
TextView userPrice = (TextView) convertView.findViewById(R.id.userPrice);
TextView userRating = (TextView) convertView.findViewById(R.id.userRating);
TextView jarak = (TextView) convertView.findViewById(R.id.jarak);
Jarak j = jarakItems.get(position);
thumbNail.setImageUrl(j.getGambar(), imageLoader);
userName.setText(j.getName());
userTim.setText(j.getTim());
userTell.setText(j.getTell());
userRating.setText(j.getRating());
userPrice.setText(j.getPrice());
jarak.setText(j.getJarak() + "กม.");
return convertView;
}
}
Date :
2017-02-15 21:05:01
By :
atikan5566
ต้องใส่โค้ดอะไร และใส่ในส่วนไหนครับ ถึงจะ สามารถ คลิ๊กสถานที่บนListviewแล้วเก็บค่าทีละ 1 ลงไปใน ฐานข้อมูลครับ
Date :
2017-02-15 21:08:15
By :
atikan5566
Code (Android-Java)
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// Show on new activity
Intent newActivity = new Intent(ListUpdateActivity.this,UpdateActivity.class);
newActivity.putExtra("MemID", MebmerList.get(position).get("MemberID").toString());
startActivity(newActivity);
}
});
ตรง setOnItemClickListener ครับ
Date :
2017-02-16 09:24:08
By :
mr.win
เอาไปเพิ่มในส่วนไหนของโค้ดผมครับ รบกวนพิมพ์ให้ผมหน่อยครับ
Date :
2017-02-16 09:47:08
By :
atikan5566
เพิ่ม Event ที่ผมเขียนให้เข้าไปครับ
Date :
2017-02-16 10:53:48
By :
mr.win
เพิ่มตรงหน้า MainActivity.java หรือ ตรงCustomAdapter ครับ
Date :
2017-02-16 12:11:49
By :
atikan5566
ลองไปอ่านพื้นฐาน ListView ก่อนนะครับ ไม่งั้คุณก็ไปต่อไม่ได้
Date :
2017-02-16 13:16:07
By :
mr.win
Load balance : Server 01