我正在开发一个应用程序,其中我使用搜索视图来显示我想要搜索的数据,但我无法执行此操作。我是android.how的新手,当数据来自服务器时使用mysql和php
来搜索数据package selva.select;
public class Worker extends Activity implements {
@Override
protected void onResume() {
// TODO Auto-generated method stub
setContentView(R.layout.worker);
SearchView search;
RelativeLayout rl;
Button logout;
logout = (Button)findViewById(R.id.button2);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it = new Intent(Worker.this,Login.class);
startActivity(it);
finish();
}
});
ZoomControls zoom;
final TableLayout tl;
zoom = (ZoomControls) findViewById(R.id.zoomControls1);
tl=(TableLayout)findViewById(R.id.table);
zoom.setOnZoomInClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
float x = tl.getScaleX();
float y = tl.getScaleY();
tl.setScaleX((float) (x+1));
tl.setScaleY((float) (y+1));
}
});
zoom.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
float x = tl.getScaleX();
float y = tl.getScaleY();
tl.setScaleX((float) (x-1));
tl.setScaleY((float) (y-1));
}
});
SearchView sv;
sv= (SearchView)findViewById(R.id.searchView1);
sv.setQueryHint("Search Worker Id");
String response = null;
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://hcm4zealit.com/PHP/worker2.php");
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
// Toast.makeText(getApplicationContext(), "pass",
// Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
Toast.makeText(getApplicationContext(), "Connection fail",
Toast.LENGTH_SHORT).show();
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
// Toast.makeText(getApplicationContext(),
// "Input Reading pass", Toast.LENGTH_SHORT).show();
}
is.close();
response = sb.toString();
// Toast.makeText(getApplicationContext(), response,
// Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
// Toast.makeText(getApplicationContext(),
// " Input reading fail", Toast.LENGTH_LONG).show();
}
// parse json data
try {
JSONObject jObject = new JSONObject(response);
JSONArray jArray = jObject.getJSONArray("result");
TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag =1;
for (int i = -1; i <= jArray.length()-1; i++) {
//JSONObject json_data = jArray.getJSONObject(i);
TableRow tr = new TableRow(Worker.this);
tr.setClickable(true);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
if(flag==1) {
TextView b = new TextView(Worker.this);
b.setText(" Id");
b.setTextSize(17);
b.setTypeface(b.getTypeface(),Typeface.BOLD);
b.setTextColor(Color.BLACK);
tr.addView(b);
TextView b1 = new TextView(Worker.this);
b1.setTextColor(Color.BLACK);
b1.setPadding(20, 20, 20, 20);
b1.setTextSize(17);
b1.setTypeface(b1.getTypeface(),Typeface.BOLD);
b1.setTextColor(Color.BLACK);
b1.setText("Name");
tr.addView(b1);
TextView b2 = new TextView(Worker.this);
b2.setTextColor(Color.BLACK);
b2.setPadding(20, 20, 20, 20);
b2.setTextSize(17);
b2.setTypeface(b2.getTypeface(),Typeface.BOLD);
b2.setText("Nationality");
tr.addView(b2);
TextView b3 = new TextView(Worker.this);
b3.setTextSize(17);
b3.setText("Company");
b3.setTextColor(Color.BLACK);
b3.setPadding(20, 20, 20, 20);
b3.setTypeface(b3.getTypeface(),Typeface.BOLD);
tr.addView(b3);
tv.addView(tr);
final View vline = new View(Worker.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.WHITE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = jArray.getJSONObject(i);
TextView b6 = new TextView(Worker.this);
// b6.setText("Worker Id");
b6.setText(json_data.getString("Worker_id"));
b6.setTextColor(Color.WHITE);
b6.setTextSize(17);
tr.addView(b6);
TextView b19 = new TextView(Worker.this);
b19.setPadding(20, 20, 20, 20);
b19.setTextSize(17);
// b19.setText("Worker Name");
b19.setText(json_data.getString("Worker_name"));
b19.setTextColor(Color.WHITE);
tr.addView(b19);
TextView b29 = new TextView(Worker.this);
b29.setPadding(20, 20, 20, 20);
// b29.setText("Nationality");
b29.setText(json_data.getString("Nationality"));
b29.setTextColor(Color.WHITE);
b29.setTextSize(17);
tr.addView(b29);
TextView b30 = new TextView(Worker.this);
b30.setPadding(20, 20, 20, 20);
// b30.setText("Company Name");
b30.setText(json_data.getString("company_name"));
b30.setTextColor(Color.WHITE);
b30.setTextSize(17);
tr.addView(b30);
tv.addView(tr);
// set TableRow onClickListner
final View vline1 = new View(Worker.this);
vline1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
}
catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail",
Toast.LENGTH_SHORT).show();
}
super.onResume();
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
private void doMySearch(String query) {
// TODO Auto-generated method stub
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
// SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//searchView.setOnQueryTextListener((OnQueryTextListener) this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#3498DB"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:background="#34495E"
android:layout_height="wrap_content" >
</FrameLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="120sp"
android:focusable="true"
android:layout_marginBottom="60dp"
android:focusableInTouchMode="true" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/textView1"
android:focusable="true"
android:focusableInTouchMode="true" >
</TableLayout>
</ScrollView>
</HorizontalScrollView>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="105dp"
android:layout_alignParentLeft="true"
android:background="@drawable/bg" >
<Button
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_marginRight="10dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/search_view_rounde_corner"
android:text="Logout" />
<SearchView
android:id="@+id/searchView1"
android:layout_width="fill_parent"
android:hint="Search id"
android:layout_marginRight="10dp"
android:background="@drawable/search_view_rounde_corner"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</SearchView>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:text="Worker Report"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F4F6F7"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right" />
</RelativeLayout>
我无法添加可以从表中搜索数据的搜索字段。我是android的新手..