gpt4 book ai didi

android - 如何在 ImageView 中显示多张图片

转载 作者:太空狗 更新时间:2023-10-29 15:07:56 26 4
gpt4 key购买 nike

我想按 rowwise 显示带有文本的图像。这是我的 xml 文件

并且在 RowItem 类中包含我设置的位图对象

现在我正在使用硬编码的字符串数组,但我想使用 URL 的字符串数组进行设置我使用 httpconnection 从网络中获取这个 url,我还在 andoridManifest.xml 文件中添加了权限

我很困惑我没有得到任何正确的解决方案请帮助我

我引用这个 url 来显示图片 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/我想做同样的事情,只使用 url 而不是硬编码图像

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />

<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:textColor="#3399FF"
android:textSize="14dp" />
</RelativeLayout>

我的 Java 代码

 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class BdaylistActivity extends Activity implements
OnItemClickListener {
private ProgressDialog m_ProgressDialog = null;
ListView listView;
List<RowItem> rowItems;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bdaylist);


m_ProgressDialog = ProgressDialog.show(this,
"Please wait...", "Retrieving data ...", true);
new BirthdayTask().execute("https://gdirectoryqa.appspot.com/gDirectory/get_user_profile.htm");


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {


Intent intent = new Intent(this, ProfileViewActivity.class);
intent.putExtra("BDay", true);
startActivity(intent);
}


class BirthdayTask extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (Exception e) {
e.printStackTrace();
}
return responseString;
}


@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

JSONArray jArr;
ArrayList<String> firstLastNames=new ArrayList<String>();
ArrayList<Integer> images=new ArrayList<Integer>();
try {
jArr = new JSONArray(result);
for (int count = 0; count < jArr.length(); count++) {
JSONObject obj = jArr.getJSONObject(count);
JSONObject userGeneralInfoArray = jArr.getJSONObject(count).getJSONObject("userGeneralInfo");
firstLastNames.add(userGeneralInfoArray.getString("firstName")+" "+userGeneralInfoArray.getString("lastName"));
images.add(R.drawable.ic_launcher);
}

System.out.println("firstName="+firstLastNames);
rowItems = new ArrayList<RowItem>();

if(firstLastNames.size() == 0){
firstLastNames.add("No Results");
images.add(1);
}
String[] images=new String[]{"http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://theopentutorials.com/wp-content/uploads/totlogo.png"};
for (int i = 0; i < firstLastNames.size(); i++) {
RowItem item = new RowItem(images.get(i), null, firstLastNames.get(i));
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(BdaylistActivity.this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(BdaylistActivity.this);
m_ProgressDialog.dismiss();

} catch (JSONException e) {
e.printStackTrace();
}



}

}


}

最佳答案

希望这可能有所帮助。

每当我想从互联网上获取图像并将其显示在我的 android 应用程序中时,我总是使用“Aquery”

https://code.google.com/p/android-query/

它速度快、易于使用,而且只需要很少的代码就可以实现奇迹。 :-)

关于android - 如何在 ImageView 中显示多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20046356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com