gpt4 book ai didi

android - 如何在android中的两个不同日期从MySQL获取数据并在多列ListView中显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:42 26 4
gpt4 key购买 nike

我正在将三个(客户名称、开始日期和结束日期)值传递给 PHP 文件,但背后的问题是,我当时没有传递三个值,只有一个值(客户名称)移动到 PHP 文件。并且如何在多列列表中显示日期

java文件:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_repo);

// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

setCurrentDateOnView();
addListenerOnButton();

cus_name = (Spinner) findViewById(R.id.spinner1);//customer spinner
created_date1 = (TextView) findViewById(R.id.tvDate);
created_date2 = (TextView) findViewById(R.id.tvDate2);

//spinner for customer name
cus_name = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(this, R.layout.spinner_custo_report, R.id.txt, listItems);
cus_name.setAdapter(adapter);
cus_name.setAdapter(new NothingSelectedSpinnerAdapter(adapter, R.layout.custo_rep_nothingselect, this));


Button submit = (Button) findViewById(R.id.loadbtn);
assert submit != null;
submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

cus_names = cus_name.getSelectedItem().toString();
createddate = created_date1.getText().toString();
created_date = created_date2.getText().toString();
String url_server = "http://192.168.1.13:8090/Vaari_services/getCustomerReportData.php";
new BackgroundTask_Asycn().execute(url_server);
}
});
}


// Class with extends AsyncTask class
//product stock one
private class BackgroundTask_Asycn extends AsyncTask<String, Void, Void> {

// Required initialization
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(Customer_Repo.this);
String cus_name ="";
String created_date1 ="";
String created_date2 ="";
int sizeData = 0;
TextView pro_stock1 = (TextView)findViewById(R.id.tvDate);
Spinner customerName = (Spinner)findViewById(R.id.spinner1);
TextView createddate1 = (TextView) findViewById(R.id.tvDate);
TextView createddate2 = (TextView) findViewById(R.id.tvDate2);

protected void onPreExecute() {
// NOTE: You can call UI Element here.
//Start Progress Dialog (Message)

Dialog.setMessage("Please wait..");
Dialog.show();

try{
// Set Request parameter product one
cus_name +="&" + URLEncoder.encode("cus_name", "UTF-8") + "="+customerName.getSelectedItem();
//created_date1 +="&" + URLEncoder.encode("created_date1", "UTF-8") + "="+createddate1.getText();
//created_date2 +="&" + URLEncoder.encode("created_date2", "UTF-8") + "="+createddate2.getText();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// Call after onPreExecute method
protected Void doInBackground(String... urls) {
/************ Make Post Call To Web Server ***********/
BufferedReader reader=null;

// Send data
try
{
// Defined URL where to send data
URL url = new URL(urls[0]);
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(cus_name);
//wr.write(created_date1);
//wr.write(created_date2);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "");
}
// Append Server Response To Content String
Content = sb.toString();
}
catch(Exception ex)
{
Error = ex.getMessage();
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
return null;
}

protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.

// Close progress dialog
Dialog.dismiss();

if (Error != null) {

pro_stock1.setText("Output : "+Error);

} else {

// Show Response Json On Screen (activity)
pro_stock1.setText( Content );

/****************** Start Parse Response JSON Data *************/

String OutputData = "";
JSONObject jsonResponse;

try {

/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject(Content);


/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("customer_repo");

/*********** Process each JSON Node ************/

int lengthJsonArr = jsonMainNode.length();

for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

/******* Fetch node values **********/
String Stock1 = jsonChildNode.optString("created_date").toString();
String Stock2 = jsonChildNode.optString("order_no").toString();
String Stock3 = jsonChildNode.optString("product").toString();
OutputData += Stock1 + Stock2 + Stock3;


}
/****************** End Parse Response JSON Data *************/

//Show Parsed Output on screen (activity)
pro_stock1.setText( OutputData );

} catch (JSONException e) {
e.printStackTrace();
}
}
}

}

这是问题的开始,如何传递我必须做的事情。如果有任何选择

cus_name +="&" + URLEncoder.encode("cus_name", "UTF-8") + "="+customerName.getSelectedItem();
//created_date1 +="&" + URLEncoder.encode("created_date1", "UTF-8") + "="+createddate1.getText();
//created_date2 +="&" + URLEncoder.encode("created_date2", "UTF-8") + "="+createddate2.getText();

这是正确的方法

    wr.write(cus_name);
//wr.write(created_date1);
//wr.write(created_date2);

XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#282828"
android:orientation="vertical"
android:padding="10dp">

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#282828"
android:padding="10dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="2dp">

<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="CUSTOMER ORDER REPORT"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#00aa55"
android:textSize="30dp"
android:textStyle="bold" />

</TableRow>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="5dp">

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">

<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:textSize="20sp" />

</TableRow>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="2dp">

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">

<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/datepicker_btn"
android:padding="5dp"
android:text="FROM DATE"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />

<Button
android:id="@+id/btnChangeDate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/datepicker_btn"
android:padding="10dp"
android:text="TO DATE"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<TextView
android:id="@+id/tvDate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />


</TableRow>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="2dp">

<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">


<Button
android:id="@+id/loadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btnbg"
android:padding="10dp"
android:text="LOAD"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
</LinearLayout>

<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- table format-->
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorCell">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="ORDER DATE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:padding="5dp"
android:text="ORDER NO"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="center"
android:padding="5dp"
android:text="PRODUCT NAME"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="ORDER QTY"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="SUPPLIED QTY"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="SUPPLIED DATE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="BALANCE QTY"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />


</LinearLayout>

</ScrollView>
</HorizontalScrollView>

<ListView
android:id="@+id/listview_customername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null" />
</TableLayout>
</RelativeLayout>

PHP 文件:

    <?php
require "db_config.php";

$cus_name= $_POST["cus_name"];
$created_date1= $_POST['created_date1'];
$created_date2= $_POST['created_date2'];

$sql="select oc.created_date,oc.order_no,ot.product,ot.order_qty,ot.qty_supply as Supplied_qty,ot.sup_date as issue_date,ot.bal_qty from order_creation oc inner join order_tran ot on oc.order_no=ot.orderno where oc.cus_name='".$cus_name."' and convert(datetime,ot.sup_date,104) between convert(datetime,'".$created_date1."') and convert(datetime, '".$created_date2."') and ot.product is not null order by ot.product,oc.order_no,ot.bal_qty desc";

$stmt=sqlsrv_query($conn,$sql );
if($stmt===false)
{
die(print_r(sqlsrv_errors(),true));
}
while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$json['customer_repo'][]=$row;
}
sqlsrv_free_stmt($stmt);
echo json_encode($json);

?>

最佳答案

对数据集使用数据模型类而不是 HashMap 。

public class CustomerData {
String createdAt;
String orderNo;

public CustomerData(String createdAt, String orderNo) {
this.createdAt = createdAt;
this.orderNo = orderNo;
}

public String getCreatedAt() {
return createdAt;
}

public String getOrderNo() {
return orderNo;
}
}

然后像这样修改你的代码

 public void ShowData() {
// listView1
final ListView lisView1 = (ListView) findViewById(R.id.listview_customername);

Spinner cus_name = (Spinner) findViewById(R.id.spinner1);
TextView created_date1 = (TextView) findViewById(R.id.tvDate);
TextView created_date2 = (TextView) findViewById(R.id.tvDate2);


String url = "http://192.168.1.13:8090/Vaari_services/getCustomerReportData.php";

// Paste Parameters
//List<NameValuePair> params = new ArrayList<NameValuePair>();

new GetCustomerDataTask().execute(url);

}
private class GetCustomerDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
parseData(result);
}
}


// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;

List<NameValuePair> params = new ArrayList<>();

params.add(new BasicNameValuePair("cus_name", cus_names);
params.add(new BasicNameValuePair("created_date1", createddate);
params.add(new BasicNameValuePair("created_date2", created_date);



try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();

// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;

// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}


public void parseData(String response){
try {
JSONObject parentObject = new JSONObject(response);
JSONArray data = parentObject.getJSONArray("customer_repo");

//JSONArray data = new JSONArray(getJSONUrl(url,params));
ArrayList<CustomerData> customerArrayList = new ArrayList<CustomerData>();
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String createdDate = c.getString("created_date");
String orderNo = c.getString("created_date");
customerArrayList.add(new CustomerData(createdDate, orderNo));
}

imageAdapter = new ImageAdapter(this, customerArrayList);
lisView1.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
registerForContextMenu(lisView1);


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public class ImageAdapter extends BaseAdapter {
private Context context;
ArrayList<CustomerData> customerDataArrayList;

public ImageAdapter(Context c,ArrayList<CustomerData> customerDataArrayList) {
// TODO Auto-generated method stub
context = c;
this.customerDataArrayList= customerDataArrayList;
}

public int getCount() {
// TODO Auto-generated method stub
return customerDataArrayList.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return customerDataArrayList.get(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_cus_report, null);
}

// created date
TextView txtOrdDate = (TextView) convertView.findViewById(R.id.ColOrDate);
txtOrdDate.setPadding(10, 0, 0, 0);
txtOrdDate.setText(customerDataArrayList.get(position).getCreatedAt() + ".");
txtOrdDate.setTextSize(18);
txtOrdDate.setTextColor(Color.parseColor("#FFFFFF"));

// Oredr No
TextView txtOrdNo = (TextView) convertView.findViewById(R.id.ColOrNo);
txtOrdNo.setPadding(5, 0, 0, 0);
txtOrdNo.setText(customerDataArrayList.get(position).getOrderNo());
txtOrdNo.setTextSize(18);
txtOrdNo.setTextColor(Color.parseColor("#FFFFFF"));

return convertView;

}

}

同时发布你的getJSONUrl方法

关于android - 如何在android中的两个不同日期从MySQL获取数据并在多列ListView中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com