Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android列表视图,带有图像和大约10个字符串_Android_Android Listview - Fatal编程技术网

Android列表视图,带有图像和大约10个字符串

Android列表视图,带有图像和大约10个字符串,android,android-listview,Android,Android Listview,我正计划开发一款应用程序,上面有图像和文字,如下所示: 在这种情况下,我应该提到我是Java和Android新手 所有细节,包括图像的URL都存储在mysql中,检索mysql记录并在Android中显示它们的php已经完成(JSON数组) 这是我的问题。Android studio强制我使用异步操作从mysql(网络)获取细节,因此我必须在一个后台函数中获取所有细节,包括转换存储为位图的图像路径。我看到的唯一选项是将列表对象从backgroundjob返回到主视图。我需要一些关于列表的帮助,

我正计划开发一款应用程序,上面有图像和文字,如下所示:

在这种情况下,我应该提到我是Java和Android新手

所有细节,包括图像的URL都存储在mysql中,检索mysql记录并在Android中显示它们的php已经完成(JSON数组)

这是我的问题。Android studio强制我使用异步操作从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>';
    }
}
if($result=mysqli\u查询($con,$sql))
{
而($row=$result->fetch_object())
{
回声“
”; $tempArray=$row; $dh=opendir('c:/wamp/www/'.$row->pic_dir); while(false!=($filename=readdir($dh))){ 如果($filename!=“.”和$filename!=“.”){ 回显“pic_dir.$filename.”“width=“40”height=“40”/>”; } } 回声'; } }
Android-MainActivity.java(它还没有完成,只是不知道如何从这里开始,列表/数组必须包含不同的对象)

package com.example.rgopalkr.readdb;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.media.Image;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.lang.String;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
类TestAsync扩展异步任务
{
受保护的void onPreExecute(){
TextView tv=(TextView)findViewById(R.id.textv);
setText(“从数据库获取描述”);
}
受保护位图doInBackground(无效…arg0){
StringBuilder str=新的StringBuilder();
位图bmp=null;
试一试{
URL=新URL(“http://10.0.2.2/shareit/php/test_json.php");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStream in=conn.getInputStream();
InputStreamReader isw=新的InputStreamReader(in);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
字符串行=”;
而((line=br.readLine())!=null){
str.append(行);
}
str.append(conn.getResponseCode());
连接断开();
}catch(IOException M){M.printStackTrace();}
试一试{
JSONArray jArray=新的JSONArray(str.toString());
StringBuilder sb=新的StringBuilder();
字符串imagePath=”http://10.0.2.2/shareit/";
for(int i=0;i
活动主要的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次才能嵌入图片)

我在这里读了这篇文章,它给了我一些想法:http-w]w[w\androidhive\info/2012/02/android-custom-listview with-image和text/,但看起来很复杂。如果有人能帮我处理多对象类型列表,并给我一些如何前进的建议,那就太棒了


此外,第一个屏幕上只会显示带有说明的缩略图,当按下该屏幕时,应显示为该记录存储的所有图像。

第一件事是不要将所有位图创建为该屏幕的一部分
<?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>
compile 'com.squareup.picasso:picasso:2.5.2'
Picasso.with(context).load([imageUri]).into(imageView);
Picasso.with(context).load([imageUri]).placeholder([placeholder res]).into(imageView);