ขอความช่วยเหลือหน่อยครับ คือต้องการแสดงจำนวนเงินแบบ มีคอมม่าใน andriod listview ต้องแก้ดค๊ตอย่างไรบ้างครับ
ตอนนี้เป็น แบบนี้ครับ
Date :
2020-03-22 15:57:32
By :
sranuwat
ลอง String.format() ดูครับ เช่น
Code (Java)
txtAmount.setText(String.format("%,d", MyArrList.get(position).get("total")));
หรือ
Code (Java)
txtAmount.setText(String.format("%,d", (int) MyArrList.get(position).get("total")));
Date :
2020-03-22 19:43:27
By :
PhrayaDev
ขอบคุณ คุณพระยาเทพ นะครับ แต่ผมลองทั้ง 2 CODE ไม่ได้ครับ รบกวนหน่อยนะครับ
Date :
2020-03-22 22:05:12
By :
sranuwat
ไม่มี error อะไรบอกเหรอครับ
ลองใช้ DecimalFormat
Code (Java)
import java.text.DecimalFormat;
//..
DecimalFormat formatter = new DecimalFormat("#,###");
formatter.format(ตัวเลขที่จะใส่คอมม่า);
Date :
2020-03-23 12:46:50
By :
PhrayaDev
ไม่มี ERROR ครับ ตอนนี้ยังไม่ได้เหทือนเดิมครับ รบกวนผู้รู้ด้วยนะครับ
โค๊ดทั้งหมดครับ
Code
package com.example.priyanka.qrbarcodescanner;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
public class PreorderActivity extends Activity {
TextView textView,textView2;
Button btscan;
ArrayList<HashMap<String, String>> MyArrList;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preorder);
// Assign ID's to textview and button.
textView = (TextView)findViewById(R.id.TextViewUserEmail);
textView2 = (TextView)findViewById(R.id.TextAmount);
btscan = (Button)findViewById(R.id.button_scan);
btscan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String TempHolder3 = getIntent().getStringExtra("MemberID");
Intent myIntent3 = new Intent(PreorderActivity.this, ScanActivity.class);
myIntent3.putExtra("MemberID", TempHolder3);
startActivity(myIntent3);
}
});
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 15) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ShowData();
// btnSearch
// final Button btnSearch = (Button) findViewById(R.id.btnSearch);
//btnSearch.setBackgroundColor(Color.TRANSPARENT);
// Perform action on click
/// btnSearch.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// ShowData();
// }
// });
}
public void ShowData()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
String url = "http://www.bp-world.com/andriod/showAllData.php";
Intent intent= getIntent();
final String shopcode = intent.getStringExtra("MemberID");
final String Barcode = intent.getStringExtra("Barcode");
final String saleqty = intent.getStringExtra("tsaleqty");
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("txtKeyword", strKeySearch.getText().toString()));
params.add(new BasicNameValuePair("shopcode", shopcode));
params.add(new BasicNameValuePair("barcode", Barcode));
params.add(new BasicNameValuePair("saleqty", saleqty));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
int SumAmount=0;
int SumQty=0;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("MemberID", c.getString("MemberID"));
map.put("Username", c.getString("Username2"));
map.put("Password", c.getString("Password"));
map.put("Name", c.getString("Name"));
map.put("total", c.getString("total"));
map.put("Qty", c.getString("Qty"));
SumQty += Integer.parseInt(c.getString("Qty"));
SumAmount += Integer.parseInt(c.getString("total"));
MyArrList.add(map);
}
// แสดงยอดรวม.
textView.setText(String.valueOf(SumQty));
textView2.setText(String.valueOf(SumAmount));
lisView1.setAdapter(new ImageAdapter(this));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void ShowData2()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// keySearch
//EditText strKeySearch = (EditText)findViewById(R.id.txtKeySearch);
// Disbled Keyboard auto focus
// InputMethodManager imm = (InputMethodManager)getSystemService(
// Context.INPUT_METHOD_SERVICE);
//imm.hideSoftInputFromWindow(strKeySearch.getWindowToken(), 0);
/**
* [{"MemberID":"1","Username":"weerachai","Password":"weerachai@1","Name":"Weerachai Nukitram","Tel":"0819876107","Email":"[email protected] "},
* {"MemberID":"2","Username":"adisorn","Password":"adisorn@2","Name":"Adisorn Bunsong","Tel":"021978032","Email":"[email protected] "},
* {"MemberID":"3","Username":"surachai","Password":"surachai@3","Name":"Surachai Sirisart","Tel":"0876543210","Email":"[email protected] "}]
*/
String url = "http://www.bp-world.com/andriod/showAllData2.php";
Intent intent= getIntent();
final String shopcode = intent.getStringExtra("MemberID");
//final String Barcode = intent.getStringExtra("Barcode");
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("txtKeyword", strKeySearch.getText().toString()));
params.add(new BasicNameValuePair("shopcode", shopcode));
//params.add(new BasicNameValuePair("barcode", Barcode));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
int SumAmount=0;
int SumQty=0;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("MemberID", c.getString("MemberID"));
map.put("Username", c.getString("Username2"));
map.put("Password", c.getString("Password"));
map.put("Name", c.getString("Name"));
map.put("total", c.getString("total"));
map.put("Qty", c.getString("Qty"));
SumQty += Integer.parseInt(c.getString("Qty"));
SumAmount += Integer.parseInt(c.getString("total"));
MyArrList.add(map);
}
// แสดงยอดรวม.
textView.setText(String.valueOf(SumQty));
textView2.setText(String.valueOf(SumAmount));
lisView1.setAdapter(new ImageAdapter(this));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
// imgCmdDelete
ImageButton cmdDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
cmdDelete.setBackgroundColor(Color.TRANSPARENT);
final AlertDialog.Builder adb1 = new AlertDialog.Builder(PreorderActivity.this);
final AlertDialog.Builder adb2 = new AlertDialog.Builder(PreorderActivity.this);
cmdDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
adb1.setTitle("Delete?");
adb1.setMessage("ยีนยันลบรายการ [" + MyArrList.get(position).get("Name") +"]\n"
+"จำนวนชิ้น [" + MyArrList.get(position).get("Qty") +"]"
);
adb1.setNegativeButton("Cancel", null);
adb1.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Request to Delete data.
String url = "http://www.bp-world.com/andriod/deleteData.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sMemberID", MyArrList.get(position).get("MemberID")));
String resultServer = getJSONUrl(url,params);
/** Get result delete data from Server (Return the JSON Code)
* StatusID = ? [0=Failed,1=Complete]
* Error = ? [On case error return custom error message]
*
* Eg Login Failed = {"StatusID":"0","Error":"Cannot delete data!"}
* Eg Login Complete = {"StatusID":"1","Error":""}
*/
String strStatusID = "0";
String strError = "Unknow Status";
try {
JSONObject c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Delete
if(strStatusID.equals("0"))
{
// Dialog
adb2.setTitle("Error! ");
adb2.setIcon(android.R.drawable.btn_star_big_on);
adb2.setPositiveButton("Close", null);
adb2.setMessage(strError);
adb2.show();
}
else
{
Toast.makeText(PreorderActivity.this, "ลบรายการสินค้าออกแล้ว.", Toast.LENGTH_SHORT).show();
ShowData2(); // reload data again
}
}});
adb1.show();
}
});
// ColMemberID
//TextView txtMemberID = (TextView) convertView.findViewById(R.id.ColMemberID);
//txtMemberID.setPadding(10, 0, 0, 0);
//txtMemberID.setText(MyArrList.get(position).get("MemberID") +".");
// R.id.ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText(MyArrList.get(position).get("Name"));
// R.id.ColTel Amount
TextView txtQty = (TextView) convertView.findViewById(R.id.ColQty);
txtQty.setPadding(5, 0, 0, 0);
txtQty.setText(MyArrList.get(position).get("Qty"));
// R.id.Amount
[font=Verdana] TextView txtAmount = (TextView) convertView.findViewById(R.id.ColAmount);
txtAmount.setPadding(5, 0, 0, 0);
txtAmount.setText(MyArrList.get(position).get("total")); [/font]
return convertView;
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
//@Override
// public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.activity_main, menu);
//return true;
}
Date :
2020-03-24 17:33:01
By :
sranuwat
คำตอบสุดท้ายครับ
Code (Java)
DecimalFormat df = new DecimalFormat("#,###");
txtAmount.setText(df.format(Integer.parseInt(MyArrList.get(position).get("total"))));
หรือ
Code (Java)
txtAmount.setText(String.format("%,d", Integer.parseInt(MyArrList.get(position).get("total"))));
https://repl.it/repls/ExoticFunctionalFeed
ประวัติการแก้ไข 2020-03-24 18:34:26
Date :
2020-03-24 18:32:39
By :
PhrayaDev
ได้แล้วครับ ขอขอบคุณ คุณพระยาเทพ มากๆ นะครับ ขอให้เจริญๆ นะครับ
Date :
2020-03-24 20:01:32
By :
sranuwat
เยี่ยม ขอให้มีความสุข สำเร็จ สมหวัง เช่นกันครับ
อย่าลืม 35155 นะครับ
ประวัติการแก้ไข 2020-03-24 20:31:43
Date :
2020-03-24 20:28:56
By :
PhrayaDev
ครับ
Date :
2020-03-24 21:24:39
By :
sranuwat
Load balance : Server 04