用于查询的 EditText 与 JSON 结果的组合显示,以便能够滚动它
Combined display of EditText for Query with JSON Result to be able to scroll it
需要有关如何定义屏幕以能够滚动 json 结果的建议,如附件:
package com.lm.vciwhereabout;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class querynameentry3 extends ListActivity {
EditText username; // To take username as input from user
Button submit;
TextView tv; // TextView to show the result of MySQL query
String returnString; // to store the result of MySQL query after decoding JSON
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network
access on the application's main thread
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jsonuse);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1));
username = (EditText) findViewById(R.id.edtxUsername);
submit = (Button) findViewById(R.id.btnsubmit);
tv = (TextView) findViewById(R.id.showresult);
// define the action when user clicks on submit button
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// declare parameters that are passed to PHP script i.e. the name "username" and its value submitted by user
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// define the parameter
postParameters.add(new BasicNameValuePair("username",
username.getText().toString()));
String response = null;
// call executeHttpPost method passing necessary parameters
try
{
response = CustomHttpClient.executeHttpPost(
"http://192.168.0.245/vciwhereabout/waboutbyuser.php", // your ip address if using localhost server
postParameters);
// store the result returned by PHP script that runs MySQL query
String result = response.toString();
//parse json data
try{
returnString = "";
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","post_id: "+json_data.getInt("post_id")+
", username: "+json_data.getString("username")+
", fdate: "+json_data.getString("fdate")+
", tdate: "+json_data.getString("tdate")+
", ftime: "+json_data.getString("ftime")+
", ttime: "+json_data.getString("ttime")+
", custbranch: "+json_data.getString("custbranch")+
", custname: "+json_data.getString("custname")+
", custaddr: "+json_data.getString("custaddr")+
", custcity: "+json_data.getString("custcity")+
", note: "+json_data.getString("note")
);
//Get an output to the screen
returnString += "Username :" + json_data.getString("username")
+ "\n" + "From Date :" + json_data.getString("fdate")
+ "\n" + "To Date :" + json_data.getString("tdate")
+ "\n" + "From Time :" + json_data.getString("ftime")
+ "\n" + "To Time :" + json_data.getString("ttime")
+ "\n" + "Cust/Branch :" + json_data.getString("custbranch")
+ "\n" + "Name :" + json_data.getString("custname")
+ "\n" + "Address :" + json_data.getString("custaddr")
+ "\n" + "City :" + json_data.getString("custcity")
+ "\n" + "Note :" + json_data.getString("note") + "\n"
+ "-------------------------------------------" ;
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
try{
tv.setText(returnString);
}
catch(Exception e){
Log.e("log_tag","Error in Display!" + e.toString());;
}
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
}
});
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
}
这是布局 activity_jsonuse.xml :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Staff Name to be Query" />
<EditText
android:id="@+id/edtxusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
</EditText>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/showresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>
在将输出输出到屏幕时,如何定义/更改它以便滚动结果?
因为我已经用 edittext 定义了查询的布局,我是否可以制作或调用新布局来仅显示具有滚动功能的结果(在一个 .java 活动中)?请帮助指导我上面的代码。
非常感谢(我是 Android 和编程方面的新手)。
加上附件中的scrollview,解决了。但是显示结果后虚拟键盘仍然出现。结果出现在屏幕上后,如何自动删除虚拟键盘?谢谢。
<ScrollView
> android:layout_width="fill_parent"
> android:lines="20"
> android:layout_height="390dip" >
> <TextView
> android:id="@+id/showresult2"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:singleLine="false"
> android:textAppearance="?android:attr/textAppearanceLarge"
>
> android:paddingBottom="2dip"
> android:paddingLeft="45dip"
> android:textColor="#5d5d5d"
>
> />
>
> </ScrollView>
添加了附件中的滚动视图,解决了我的问题。
<ScrollView
> android:layout_width="fill_parent"
> android:lines="20"
> android:layout_height="390dip" >
> <TextView
> android:id="@+id/showresult2"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:singleLine="false"
> android:textAppearance="?android:attr/textAppearanceLarge"
>
> android:paddingBottom="2dip"
> android:paddingLeft="45dip"
> android:textColor="#5d5d5d"
>
> />
>
> </ScrollVi
新>
需要有关如何定义屏幕以能够滚动 json 结果的建议,如附件:
package com.lm.vciwhereabout; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class querynameentry3 extends ListActivity { EditText username; // To take username as input from user Button submit; TextView tv; // TextView to show the result of MySQL query String returnString; // to store the result of MySQL query after decoding JSON /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network
access on the application's main thread .penaltyLog().build()); super.onCreate(savedInstanceState); setContentView(R.layout.activity_jsonuse);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1)); username = (EditText) findViewById(R.id.edtxUsername); submit = (Button) findViewById(R.id.btnsubmit); tv = (TextView) findViewById(R.id.showresult); // define the action when user clicks on submit button submit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // declare parameters that are passed to PHP script i.e. the name "username" and its value submitted by user ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); // define the parameter postParameters.add(new BasicNameValuePair("username", username.getText().toString())); String response = null; // call executeHttpPost method passing necessary parameters try { response = CustomHttpClient.executeHttpPost( "http://192.168.0.245/vciwhereabout/waboutbyuser.php", // your ip address if using localhost server postParameters); // store the result returned by PHP script that runs MySQL query String result = response.toString(); //parse json data try{ returnString = ""; JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","post_id: "+json_data.getInt("post_id")+ ", username: "+json_data.getString("username")+ ", fdate: "+json_data.getString("fdate")+ ", tdate: "+json_data.getString("tdate")+ ", ftime: "+json_data.getString("ftime")+ ", ttime: "+json_data.getString("ttime")+ ", custbranch: "+json_data.getString("custbranch")+ ", custname: "+json_data.getString("custname")+ ", custaddr: "+json_data.getString("custaddr")+ ", custcity: "+json_data.getString("custcity")+ ", note: "+json_data.getString("note") ); //Get an output to the screen returnString += "Username :" + json_data.getString("username") + "\n" + "From Date :" + json_data.getString("fdate") + "\n" + "To Date :" + json_data.getString("tdate") + "\n" + "From Time :" + json_data.getString("ftime") + "\n" + "To Time :" + json_data.getString("ttime") + "\n" + "Cust/Branch :" + json_data.getString("custbranch") + "\n" + "Name :" + json_data.getString("custname") + "\n" + "Address :" + json_data.getString("custaddr") + "\n" + "City :" + json_data.getString("custcity") + "\n" + "Note :" + json_data.getString("note") + "\n" + "-------------------------------------------" ; } } catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } try{ tv.setText(returnString); } catch(Exception e){ Log.e("log_tag","Error in Display!" + e.toString());; } } catch (Exception e) { Log.e("log_tag","Error in http connection!!" + e.toString()); } } }); } private void setListAdapter(ArrayAdapter<String> arrayAdapter) { // TODO Auto-generated method stub } }
这是布局 activity_jsonuse.xml :
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Staff Name to be Query" /> <EditText android:id="@+id/edtxusername" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" > </EditText> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:layout_gravity="center" /> <TextView android:id="@+id/showresult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Result" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
在将输出输出到屏幕时,如何定义/更改它以便滚动结果?
因为我已经用 edittext 定义了查询的布局,我是否可以制作或调用新布局来仅显示具有滚动功能的结果(在一个 .java 活动中)?请帮助指导我上面的代码。
非常感谢(我是 Android 和编程方面的新手)。
加上附件中的scrollview,解决了。但是显示结果后虚拟键盘仍然出现。结果出现在屏幕上后,如何自动删除虚拟键盘?谢谢。
<ScrollView
> android:layout_width="fill_parent"
> android:lines="20"
> android:layout_height="390dip" >
> <TextView
> android:id="@+id/showresult2"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:singleLine="false"
> android:textAppearance="?android:attr/textAppearanceLarge"
>
> android:paddingBottom="2dip"
> android:paddingLeft="45dip"
> android:textColor="#5d5d5d"
>
> />
>
> </ScrollView>
添加了附件中的滚动视图,解决了我的问题。
<ScrollView
> android:layout_width="fill_parent"
> android:lines="20"
> android:layout_height="390dip" >
> <TextView
> android:id="@+id/showresult2"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:singleLine="false"
> android:textAppearance="?android:attr/textAppearanceLarge"
>
> android:paddingBottom="2dip"
> android:paddingLeft="45dip"
> android:textColor="#5d5d5d"
>
> />
>
> </ScrollVi
新>