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

时间:2015-08-13 06:01:35

标签: php android mysql android-layout

伙计我没有适当的方法在Android应用程序的列表视图中加载图像。我必须使用数据库中的文本加载列表中的图像。 我使用的数据库是MYSQL和PHP Server。

请帮助我。

这是我的代码。

活动类:

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 Server脚本:

<?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中的列表及其文本。谢谢

1 个答案:

答案 0 :(得分:1)

首先,您必须制作自定义适配器,您可以谷歌搜索如何在Android中制作自定义适配器,然后创建一个描述您的杂志的Item类。 为了显示图像,您可以使用Piccaso Piccasso Image Loading库简单地从服务器中显示图像。

一步一步

  1. 为杂志制作课程

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

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

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

  5. 是的。享受....