2015-10-08 22 views
0

我在具有與圖像的應用程序,然後文本,像這樣規劃:Android的列表視圖與圖像和大約10串

End state list view

在這種狀態下,我應該提到我是新來的Java和Android 。

的所有詳細信息,包括圖像的URL存儲在MySQL,PHP的檢索MySQL的記錄和顯示的Android他們完成(JSON陣列)

這裏是我的問題。 Android工作室迫使我進行異步操作以獲取來自mysql(網絡)的詳細信息,因此我必須在一個後臺函數中獲取所有細節,包括將存儲爲位圖的圖像路徑進行轉換。我所看到的唯一選擇是將列表對象從backgroundjob返回到主視圖。我需要一些幫助與清單,也許自定義適配器有一個ImageView和其他10個字符串。我迄今所做的:

PHP代碼:

<?php 

$con=mysqli_connect("localhost","user","","mydb"); 

if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result=array(); 
$sql = "SELECT * FROM temp limit 1"; 
$query_exec = mysqli_query($con,$sql) or die(mysql_error()); 

while($row=mysqli_fetch_assoc($query_exec)) { 
    $row['Image']='/Images/Myimage/2/BourneIdentity.jpg'; 
    $result[] = $row; 
} 

$json_result = json_encode($result); 

echo $json_result; 

mysqli_close($con); 
?> 

請注意:我已經硬編碼的測試圖像的位置,我會得到的文件夾中的所有圖像,像這樣:

if ($result = mysqli_query($con, $sql)) 
{ 
    while($row = $result->fetch_object()) 
    { 
     echo '<br>'; 
     $tempArray = $row; 
     $dh = opendir('c:/wamp/www/'.$row->pic_dir); 
     while (false !== ($filename = readdir($dh))) { 
      if ($filename !="." and $filename != "..") { 
       echo '<img src="' .$row->pic_dir.$filename.'" width="40" height="40"/>' ; 
      } 
     } 
     echo '<a href="'.'test.php'.'">' . $row->description .'</a>'; 
    } 
} 

的Android - MainActivity.java(這不是接近完成任何地方,只是不知道如何從這裏走具有包含不同的對象列表/數組)

package com.example.rgopalkr.readdb; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.media.Image; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.lang.String; 


public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    class TestAsync extends AsyncTask<Void, Integer, Bitmap> 
    { 
     protected void onPreExecute(){ 
      TextView tv = (TextView) findViewById(R.id.textv); 
      tv.setText("Get Discription from database"); 

     } 

     protected Bitmap doInBackground(Void...arg0) { 
      StringBuilder str= new StringBuilder(); 
      Bitmap bmp = null; 
      try { 
       URL url = new URL("http://10.0.2.2/shareit/php/test_json.php"); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       InputStream in = conn.getInputStream(); 
       InputStreamReader isw = new InputStreamReader(in); 
       BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
       String line = ""; 
       while ((line = br.readLine()) != null) { 
        str.append(line); 
       } 
       str.append(conn.getResponseCode()); 
       conn.disconnect(); 
      } catch (IOException M) {M.printStackTrace();} 

      try { 
       JSONArray jArray = new JSONArray(str.toString()); 
       StringBuilder sb = new StringBuilder(); 
       String imagePath="http://10.0.2.2/shareit/"; 
       for (int i=0; i < jArray.length(); i++) 
       { 
        try { 
         JSONObject oneObject = jArray.getJSONObject(i); 
         imagePath += oneObject.getString("Image"); 
         URL bmpurl = new URL(imagePath); 
         bmp = BitmapFactory.decodeStream(bmpurl.openConnection().getInputStream()); 
//      iv.setImageBitmap(bmp); 
         sb.append(imagePath + ','); 
        } catch (JSONException | IOException e) {e.printStackTrace();} 
       } 
//    tv.setText(sb.toString()); 
      } catch (JSONException JE) {JE.printStackTrace();} 
      return bmp; 
     } 

     protected void onProgressUpdate(Integer...a){ 

     } 

     protected void onPostExecute(Bitmap result) { 
      TextView tv = (TextView) findViewById(R.id.textv); 
      ImageView iv = (ImageView) findViewById(R.id.imageView); 
      iv.setImageBitmap(result); 
     } 
    } 

    public void loadItems(View v){ 
     Button button = (Button) findViewById(R.id.tButton); 
     TextView tv = (TextView) findViewById(R.id.textv); 
     new TestAsync().execute(); 

    } 
    public void Messagebox(int msg) { 
     Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); 
    } 


} 

活動主要的XML代碼,我想我知道我在XML文件中爲列表視圖所做的更改,我當時玩的是不同的視圖。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <Button 
     android:id="@+id/tButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Press to Load" 
     android:onClick="loadItems" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 
    <TextView 
     android:id="@+id/textv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="false" 
     android:text="This is where the text will come" 
     android:layout_centerVertical="@+id/imageView" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/lending" 
     android:id="@+id/imageView" /> 



</RelativeLayout> 

如何,現在的仿真器看起來(新手,對不起着嵌入圖像的是,必須得到10個代表嵌入圖片)

How my current emulator screen looks like

我讀的文章在這裏,這給了我一些想法:http:// w androidwive info/2012/02/android-custom-listview -with-image-and-text /,但看起來很複雜。如果任何人都可以幫助我使用多個對象類型列表,並給出一些指示如何前進,那將是非常棒的!

此外,第一個屏幕只會顯示帶有說明的縮略圖,當按下該屏幕時,應該顯示爲該記錄存儲的所有圖像。

+0

我處於與您類似的位置。如果您的問題涉及列表視圖,您是否嘗試過自定義數組適配器? http://stackoverflow.com/questions/6793999/android-listview-with-2-textviews-per-item –

+0

@ScriptKitty ListView是舊的方式。 RecyclerView是創建列表的新方法。我用這個文檔來創建一個RecyclerView https://developer.android.com/training/material/lists-cards.html – GKproggy

+0

@GKproggy,RecyclerView據我所知是你如何顯示數據加載後,我的問題是更多如何加載數據,但感謝您的建議。當我有所有的數據顯示 – GreenThumb

回答

0

首先是不要創建所有的位圖作爲json解析的一部分。出現在列表視圖中的圖像應該被延遲加載。

這意味着你所需要做的就是將圖像的URL解析回UI線程。

由於用戶滾動的圖像將在此時加載。

看看一個名爲畢加索的圖書館,它會爲你加載。

在你gradle這個文件下的相關性添加

compile 'com.squareup.picasso:picasso:2.5.2' 

然後在getView列表視圖適配器添加

Picasso.with(context).load([imageUri]).into(imageView); 

您還可以,如果你想與

Picasso.with(context).load([imageUri]).placeholder([placeholder res]).into(imageView); 
添加一個佔位符

底座適配器的完整代碼

How to inflate views programmatically in vertical order

+0

好吧,我會使用RecyclerView。我會試一試並更新它是如何發生的。你會有樣品代碼嗎?來自Python,Java看起來像寫了一個有很多行和文件的書:( – GreenThumb

+0

它工作得很好!我現在可以獲取包括URL在內的所有列,並使用Picasso從URL加載圖像我需要一種方法來加載所有列表中的行並顯示爲可滾動的 – GreenThumb

+0

查看我的答案底部的鏈接,瞭解如何使用listview – Stimsoni