需要获取用户通过android应用提供的subject
,然后用它来搜索数据库以找到{{1}的teacher
name
和email
}},然后将其subject
,email
发送到Android列表视图。
PHP代码:
name
适配器
if ( !empty($_GET['subject']) ) {
$SUBJECT=$_GET['subject'];
$sql="SELECT IFF(sub== Name,Email,subjects_known FROM teacher_registration";
$res=$con->query($sql);
$result["Email"] = array();
$result["Name"] = array();
$result["Sub"] = array();
$row=array();
if($res->num_rows>0){
while($row=$res->fetch_assoc())
{
//$Name= array();
$Name= $row['Name'];
//array_push($result["Name"], $Name);
// $Email=array();
$Email= $row['Email'];
$Sub=$row['subjects_known'];
array_push($result["Email"],$Email);
array_push($result["Name"], $Name);
array_push($result["Sub"],$Sub);
// $result[] = $row;
}
// $r1=array();
// $r1['TEACHER']=$result;
echo json_encode($result,JSON_FORCE_OBJECT);
mysqli_close($con);
}
else
{
echo "Give the info";
mysqli_close($con);
}
搜索启动器:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomListView extends BaseAdapter {
public String[] profilename;
public String[] Email;
LayoutInflater li;
public CustomListView(String[] p,String[] e,Teacher_searching ref) {
Context context = ref;
li=LayoutInflater.from(context);
this.profilename=p;
this.Email=e;
}
@Override
public int getCount() {
return Email.length;
}
@Override
public Object getItem(int position) {
return (position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
ViewHolder v=null;
if(convertView==null){
convertView = li.inflate(R.layout.layout, null);
v=new ViewHolder(convertView);
convertView.setTag(v);
}
else {
v=(ViewHolder)convertView.getTag();
}
v.tvw1.setText(profilename[position]);
v.tvw2.setText(Email[position]);
return convertView;
}
class ViewHolder{
TextView tvw1;
TextView tvw2;
ViewHolder(View v){
tvw1=v.findViewById(R.id.tvprofilename);
tvw2=v.findViewById(R.id.tvemail);
}
}
}
搜索::
public class Teacher_searching extends AppCompatActivity {
String urladdress="https://light-headed-nests.000webhostapp.com/proed/searching1.php";
String[] name;
String[] Email;
String SUBJECT_SEARCH;
ListView listView;
BufferedInputStream is;
JsonObjectRequest jsonObjectRequest;
String subject_Search;
String line;
String result=null;
Bundle b=new Bundle();
JSONObject jsonObj = new JSONObject();
RequestQueue mqueue;
StringRequest s;
Integer i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_searching);
listView=(ListView) findViewById(R.id.lview);
SUBJECT_SEARCH=b.getString("Teacher");
// collectData();
mqueue= Volley.newRequestQueue(getApplicationContext());
jsonParse();
// onlyvolley();
Toast.makeText(getApplicationContext(),Email.length,Toast.LENGTH_SHORT).show();
/*CustomListView customListView=new CustomListView(name,Email,Teacher_searching.this);
listView.setAdapter(customListView);*/
}
/* private void onlyvolley() {
s=new StringRequest(Request.Method.POST, urladdress,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
showMsg("Server Response","Page not reachable");
}
)
}*/
private void jsonParse() {
jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, urladdress, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray("TEACHER");
jsonObj=null;
name=new String[jsonArray.length()];
Email=new String[jsonArray.length()];
for (int i=0;i<=jsonArray.length();i++)
{
jsonObj=jsonArray.getJSONObject(i);
name[i]=jsonObj.getString("Name");
Email[i]=jsonObj.getString("Email");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("subject",SUBJECT_SEARCH);
return params;
}
};
mqueue.add(jsonObjectRequest);
}
/*private void collectData() {
try{
URL url=new URL(urladdress);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
subject_Search=b.getString("Teacher");
jsonObj.put("subject",subject_Search);
is=new BufferedInputStream(con.getInputStream());
}
catch (Exception ex) {}
//content
try{
InputStreamReader read=new InputStreamReader(is);
BufferedReader br=new BufferedReader(read);
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null){
sb.append(line).append("\n");
}
is.close();
result=sb.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
//JSON
try{
JSONArray ja=new JSONArray("TEACHER");
JSONObject jo;
name=new String[ja.length()];
Email=new String[ja.length()];
ArrayList<HashMap<String, String>> mylist =new ArrayList<HashMap<String, String>>();
for(i=0;i<=ja.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
jo=ja.getJSONObject(i);
name[i]=jo.getString("Name");
Email[i]=jo.getString("Email");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}*/
}
列表视图中的内容:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Search_teacher extends AppCompatActivity {
Spinner sub;
Button btn;
String sub_search[]={"Physics","Chemistry","Mathematics","Biology","English","Computer(C++)","Computer(Java)","History&Civics","Geography","Economics"};
String Sub_stud;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_teacher);
sub=findViewById(R.id.subject_search);
btn=findViewById(R.id.btn);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,sub_search);
sub.setAdapter(adapter2);
sub.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0)
Sub_stud="Physics";
else if(i==1)
Sub_stud="Chemistry";
else if(i==2)
Sub_stud="Mathematics";
else if(i==3)
Sub_stud="Biology";
else if(i==4)
Sub_stud="English";
else if(i==5)
Sub_stud="Computer(C++)";
else if(i==6)
Sub_stud="Computer(Java)";
else if(i==7)
Sub_stud="History&Civics";
else if(i==8)
Sub_stud="Geography";
else if(i==9)
Sub_stud="Economics";
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void subject_teacher_search(View view) {
Intent i=new Intent(getApplicationContext(),Teacher_searching.class);
Bundle bundle=new Bundle();
bundle.putString("Teacher",Sub_stud);
i.putExtras(bundle);
startActivity(i);
}
}
列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvprofilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="16dp"
android:textColor="#333"
android:textSize="22dp"
/>
<TextView
android:id="@+id/tvemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="50dp"
android:textColor="#333"
android:textSize="22dp"
/>
</LinearLayout>
答案 0 :(得分:0)
似乎您的php代码中出现“ sql语法错误”,无论如何首先要适当地解决它,然后开始在应用程序中执行以下步骤
步骤1:使用“ 排球库”创建请求队列以从服务器检索json响应
第2步:在布局文件中添加“ Custom_Listview”
第3步:最后将“ 适配器类”设置为列表视图。就是这样
快乐编码!