简体   繁体   English

如何将ListView中的TextView传递回PHP

[英]How to pass the textview in listview back to php

For this function, I need to pass the item mentioned in the custom listview from Activity1.java back to php in order to retrieve more information (including image via Picasso library) about that particular item in another activity from mysql, so may I know whether the php file is correct and how should I open Activity2.java . 对于此功能,我需要将Activity1.java自定义listview中提到的项目传递回php,以便从mysql的另一个活动中检索有关该特定项目的更多信息(包括通过Picasso库获取的图像),所以我可以知道是否的php文件是正确的,我应该如何打开Activity2.java

Activity1.java Activity1.java

public class Activity1 extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lv= (ListView) findViewById(R.id.listview);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
            String name =((TextView) findViewById(R.id.itemname)).getText().toString();
            Intent intent = new Intent(Activity1.this, Activity2.class);
            intent.putExtra("NAME", name);
            startActivity(intent);
        }
    });
}

} }

Detail.php Detail.php

<?php
include 'dbConnect.php';
mysqli_set_charset($conn, 'utf8');
$NAME = $_POST['NAME'];

$sql = "SELECT * FROM items WHERE item_name = '$NAME'" ;
result = $conn->query($sql);

if ($result->num_rows >0) {
    while($row[] = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);  
    }
} else {
     echo "No Results Found.";
}
echo $json;
$conn->close();
?>

I would recommend you to use Retrofit 2 library for making REST requests, which is exactly what you need. 我建议您使用Retrofit 2库来发出REST请求,这正是您所需要的。

First add following dependencies to your build.gradle file 首先将以下依赖项添加到您的build.gradle文件中

dependencies {  
    // Retrofit & OkHttp
    compile 'com.squareup.retrofit2:retrofit:2.3.0'

    // JSON Converter
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
}

You will need to make an interface to describe your Retrofit service. 您将需要创建一个界面来描述您的翻新服务。 In your case it would look something like this (dont put / at the beginning of the path): 在您的情况下,它看起来像这样(不要在路径的开头放置/):

public interface PhpService {
    @FormUrlEncoded
    @POST("path/to/your/web/resource")
    Call<Item> searchForItem(@Field("NAME") String itemName);
}

You need to make a class describing your item (you will have to write that class yourself, as I dont know actual fields): 您需要制作一个描述您的商品的类(您必须自己编写该类,因为我不知道实际字段):

class Item {
    @SerializedName("name")
    String name;

    @SerializedName("url")
    String imageUrl;
}

Now you need to create actual service like so (replace baseUrl with your website): 现在,您需要像这样创建实际的服务(将baseUrl替换为您的网站):

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://yourwebsite.com/")
.build();

PhpService service = retrofit.create(PhpService.class);

Now, using created service you can make a request to your php webserver: 现在,使用创建的服务,您可以向php网络服务器发出请求:

Call<Item> item = service.searchForItem("yourItemName");

call.enqueue(new Callback<Item>() {  
   @Override
   public void onResponse(Call<Item> call, Response<Item> response) {
       if (response.isSuccessful()) {
           Itemitem = response.body();
           // todo do stuff with your item
       }
   }

   @Override
   public void onFailure(Call<Item> call, Throwable t) {
       // the network call was a failure
       // TODO: handle error
    }
});

That should be all to get item from your php script. 那应该是所有从您的php脚本中获取项目。

If you need more help check out this tutorial by futurestud.io . 如果您需要更多帮助,请查阅futurestud.io的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM