php - 从服务器上的数据库加载列表中的图像

标签 php android mysql android-layout

伙计们,我没有合适的方法在 Android 应用程序的 ListView 中加载图像。我必须将图像与数据库中的文本一起加载到列表中。 我使用的数据库是MYSQL和PHP Server。

请帮助我。

这是我的代码。

Activity 类别:

public class DisplayMagazine extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> magazinesList;
int year;
private static final String TAG_SUCCESS = "success";
private static final String TAG_MAGAZINE = "magazines";
private static final String TAG_MAGAZINE_IMAGE="magazineImage";
private static final String TAG_MONTH = "month";
private static final String TAG_PATH = "path";
JSONArray magazines = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_magazine);
    Intent i = getIntent();
    year = i.getIntExtra("year", 0);
    magazinesList = new ArrayList<>();
    new LoadAllMagazines().execute();
    ListView lv = getListView();
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TextView textView = (TextView) findViewById(R.id.path);
                    textView.setClickable(true);
                    textView.setMovementMethod(LinkMovementMethod.getInstance());
                    String text = "https://docs.google.com/gview?embedded=true&url="+ textView.getText();
                textView.setText(Html.fromHtml(text));
            }
        });
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 100) {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}
class LoadAllMagazines extends AsyncTask<String, String, String> {
   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(DisplayMagazine.this);
        pDialog.setMessage("Loading Magazines. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("year", String.valueOf(year)));
        //String url_all_magazines = "http://172.16.26.190/shifaspeaks/get_all_magazines.php";
        String url_all_magazines = "http://10.3.1.117/shifaspeaks/get_all_magazines.php";
        //String url_all_magazines = "http://192.168.1.4/shifaspeaks/get_all_magazines.php";
        JSONObject json = jParser.makeHttpRequest(url_all_magazines, "GET", params);
        Log.d("All Magazines: ", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                magazines = json.getJSONArray(TAG_MAGAZINE);
                    for (int i = 0; i < magazines.length(); i++) {
                    JSONObject c = magazines.getJSONObject(i);
                    String month = c.getString(TAG_MONTH);
                    String path = c.getString(TAG_PATH);
                    //String imageString=c.getString(TAG_MAGAZINE_IMAGE);
                    //byte[] image= Base64.decode(imageString.getBytes(), Base64.DEFAULT);
                    //Bitmap decodedByte= BitmapFactory.decodeByteArray(image,0,image.length);
                    HashMap<String, String> map = new HashMap<>();
                    map.put(TAG_MONTH, month);
                    map.put(TAG_PATH, path);
                    //map.put(TAG_MAGAZINE_IMAGE,decodedByte.toString());
                    magazinesList.add(map);
                }
            } else {
                Intent i = new Intent(getApplicationContext(),
                        MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(
                        DisplayMagazine.this, magazinesList,
                        R.layout.list_item2, new String[] {TAG_MAGAZINE_IMAGE, TAG_MONTH,
                        TAG_PATH},
                        new int[] {R.id.magazineImage, R.id.month, R.id.path});
                setListAdapter(adapter);
            }
        });
    }
}

及其 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

及其列表布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >



<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
    android:id="@+id/month"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

<!-- Name Label -->
<TextView
    android:id="@+id/path"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:clickable="true"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17dip"
    android:textStyle="bold" />

和 PHP 服务器脚本:

<?php
mysql_connect('127.0.0.1:3306','root','');
mysql_select_db("shifaspeaks");
$response = array(); 
if(isset($_GET["year"])){
$year = $_GET["year"];
$result = mysql_query("SELECT month, path, magazineImage FROM magazine where year='$year'") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
    $response["magazines"] = array();
    while ($row = mysql_fetch_array($result)) {
        $magazine = array();
        $magazine["month"] = $row["month"];
        $magazine["path"] = $row["path"];
        //$magazine["magazineImage"]=$row["magazineImage"];
        array_push($response["magazines"], $magazine);
    }
    $response["success"] = 1;
    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No magazines found";
    echo json_encode($response);    
}

记住我必须将图像及其文本从数据库加载到 android 中的列表中。谢谢您

最佳答案

嗨,首先你必须制作自定义适配器,你可以通过谷歌搜索如何在 android 中制作自定义适配器,然后制作一个描述你的杂志的 Item 类。 要显示图像,您可以使用 Piccaso Piccasso Image Loading库只是为了显示来自服务器的图像。

一步一步

  1. 为杂志制作类(class)

  2. 制作列表项布局(包含图像、名称和路径)

  3. 制作使用上述布局和 MAGAZINE 类的列表项的自定义适配器

  4. 在自定义适配器的 getView 方法中,使用任何图像加载库从服务器加载图像。

就是这样。享受....

关于php - 从服务器上的数据库加载列表中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980536/

相关文章:

php - 图片/文件上传不适用于 materializecss 框架

java - 缩小android中后退按钮和图标之间的差距

android - 无法在芯片中添加图标

java - 在单个 Activity 中使用 2 个或更多 Firebase 实时数据库

python - 在 sqlalchemy 核心中使用 IFNULL

php - 如何连接共享 1 列且 ID 相同的 2 个表并向其中添加 WHERE 子句

php - 如何在 ajax url 中发布带空格的波斯语句

mysql - SQL SELECT 从具有特殊条件的同一个表中生成附加列

java - 无法再连接到我的 sql docker 容器

javascript - 从 php 表单提交调用 javascript 函数