无法使用简单适配器在列表视图中显示位图图像数组
unable to display array of bitmap images in listview using simple adapter
我是 android 编程新手。所以我需要帮助解决我面临的问题。
实际上,我正在使用 Eclipse 在 android 应用程序中从 magento 获取数据。我正在使用简单的适配器在列表视图中显示文本和图像。文本数据在列表视图中完美显示,但我在 listview.What 中显示图像时遇到问题我所做的是显示图像是:-
MainActivity.java class
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.
您需要使用自定义适配器在列表视图中显示图像和文本...
我正在发布这个 Link..请检查这个...
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/
我是 android 编程新手。所以我需要帮助解决我面临的问题。 实际上,我正在使用 Eclipse 在 android 应用程序中从 magento 获取数据。我正在使用简单的适配器在列表视图中显示文本和图像。文本数据在列表视图中完美显示,但我在 listview.What 中显示图像时遇到问题我所做的是显示图像是:- MainActivity.java class
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.
您需要使用自定义适配器在列表视图中显示图像和文本... 我正在发布这个 Link..请检查这个...
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/