gpt4 book ai didi

android - 无法使用简单适配器在 ListView 中显示位图图像数组

转载 作者:行者123 更新时间:2023-11-30 02:21:45 25 4
gpt4 key购买 nike

我是 android 编程的新手。所以我需要帮助解决我面临的问题。 实际上我正在使用 eclipse 在 android 应用程序中从 magento 获取数据。我正在使用简单的适配器在 ListView 中显示文本和图像。文本数据在 ListView 中完美显示,但我在 ListView 中显示图像时遇到问题。我所做的是显示图像:- MainActivity.java

public class MainActivity extends Activity {


private static final String NAMESPACE = "urn:Magento";
private static final String URL = "url of magento";
Bitmap bitmap;
URL imageURL = null;

ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();

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


StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
TextView tv1 = (TextView) findViewById(R.id.tv);
ListView lv = (ListView) findViewById(R.id.lv);
ImageView image_view = (ImageView)findViewById(R.id.imageview);
try {

SoapSerializationEnvelope env = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;

SoapObject request = new SoapObject(NAMESPACE, "login");

request.addProperty("username", "cheker");
request.addProperty("apiKey", "123456");

env.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,3600000);

androidHttpTransport.call("", env);
Object result = env.getResponse();

Log.d("sessionId", result.toString());

SoapSerializationEnvelope env1 = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

env1.dotNet = false;
env1.xsd = SoapSerializationEnvelope.XSD;
env1.enc = SoapSerializationEnvelope.ENC;

SoapObject request1 = new SoapObject(NAMESPACE, "login");

request1.addProperty("username", "cheker");
request1.addProperty("apiKey", "123456");

env1.setOutputSoapObject(request1);

HttpTransportSE androidHttpTransport1 = new HttpTransportSE(URL,60000);

androidHttpTransport1.call("", env1);
Object result2 = env1.getResponse();

String sessionId = result.toString();
request = new SoapObject(NAMESPACE, "catalogProductList");
request.addProperty("parentCategory","2" );
request.addProperty("sessionId",sessionId );
request.addProperty("productId",1 );

env.setOutputSoapObject(request);
androidHttpTransport.call("", env);


Object result1 = env.getResponse();
ArrayList<String> ar = new ArrayList<String>();

for (int i = 0; i <((SoapObject) result1).getPropertyCount(); i++)
{
Object property = ((SoapObject) result1).getProperty(i);
if (property instanceof SoapObject)
{
SoapObject category_list = (SoapObject) property;
String mStringSchoolName = category_list.getProperty("name").toString();
// String mStringSchoolGrade = category_list.getProperty("category_id").toString();
// String mStringSchoolType = category_list.getProperty("parent_id").toString();


System.out.println("mStringSchoolName "+mStringSchoolName);
// System.out.println("mStringSchoolGrade "+mStringSchoolGrade);
// System.out.println("mStringSchoolType"+mStringSchoolType);
ar.add(mStringSchoolName+"\n\n");

}


}


for (int j = 0; j <((SoapObject) result1).getPropertyCount(); j++)
{

request = new SoapObject(NAMESPACE, "catalogProductAttributeMediaList");
request.addProperty("sessionId",sessionId );
request.addProperty("product",j+1 );
env.setOutputSoapObject(request);
androidHttpTransport.call("", env);
Object resultData1 = env.getResponse();
// System.out.println("mStringSchoolName "+resultData1);
Object property = ((SoapObject) resultData1).getProperty(0);
String url=(((SoapObject)property).getProperty("url").toString());
try {
imageURL = new URL(url);
}

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

try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
bitmapArray.add(bitmap);
}
catch (IOException e) {

e.printStackTrace();
}

}


List<HashMap<String, Bitmap>> list= new ArrayList<HashMap<String,Bitmap>>();

for(Bitmap var: bitmapArray)
{
HashMap<String,Bitmap> map= new HashMap<String,Bitmap>();
System.out.println("Item is: " + var);

image_view.setImageBitmap(var);

map.put("key",var);

list.add(map);

}


String[] from = { "key" };
int[] to = {R.id.imageview};

SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.activity_main,from,to);
ListView listView = (ListView) findViewById(R.id.lv);
listView.setAdapter(adapter);

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


}

}

here in output i only get last image by using **image_view.setImageBitmap(var);** in hashmap because of not adding this in listview.but i don't want this. what i want is to display images from bitmap array that contain array of bitmaps(images) in listview one by one at run time i.e load one by one using simple adapter.
so please suggest me something to acheive above mentioned requirement.I will be very thankful for your suggestions.

最佳答案

您需要使用自定义适配器在 ListView 中显示图像和文本...我正在发布此链接..请查看此链接...

http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

关于android - 无法使用简单适配器在 ListView 中显示位图图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410375/

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