将位图图像放入列表视图的图像视图中
我想将位图图像放入ListView的ImageView中。我将图像从 url 更改为位图图像。我有 10 张图像,我必须将图像放入 ListView 的每个项目中。除了惰性适配器还有其他方法吗?提前致谢!!!
这是我的代码
public class Propertylist extends ListActivity {
String proptype;
String prop;
String estimate;
String photo;
String get;
String[] data;
TextView text;
URL aURL;
InputStream is = null;
String result = "";
JSONObject jArray = null;
//Hashtable<String,Bitmap> imagemap;
private ArrayList<NameValuePair> nameValuePairs;
private LayoutInflater inflater;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
text = (TextView) findViewById(R.id.text);
Toast.makeText(getApplicationContext(), "Displaying popertylist for zipcode "+get, Toast.LENGTH_LONG).show();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//ArrayList<Hashtable<String, Bitmap>> mylist1 = new ArrayList<Hashtable<String, Bitmap>>();
Bundle bundle = this.getIntent().getExtras();
get = bundle.getString("name");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.propertyhookup.com/mobile/propertylist.php");
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("zipcode", get.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
if(result.length()<= 7){
Toast.makeText(getApplicationContext(), "No properties for this zipcode or check your zipcode ", Toast.LENGTH_LONG).show();
text.setText("No properties for this zipcode or check your zipcode");
}
else{
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
//JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.1.111/propertyhookup.com/mobile/propertylist.php");
try{
JSONArray earthquakes = jArray.getJSONArray("earthquakes");
for(int i=0;i<10;i++){
HashMap<String, String> map = new HashMap<String, String>();
//imagemap = new Hashtable<String, Bitmap>();
JSONObject e = earthquakes.getJSONObject(i);
if(e.getString("property_type").contains("1")) {
proptype ="Single Family Home";
}else if(e.getString("property_type").contains("2")) {
proptype="Condo";
}else if(e.getString("property_type").contains("3")) {
proptype="Townhouse";
}
if(e.getString("estimated_price").contains("0")) {
estimate = "Not Enough Market Value";
//estimat = (TextView) findViewById(R.id.estimat);
//estimat.setTextColor(Color.rgb(0, 0, 23));
}else {
estimate = "$"+e.getString("estimated_price");
}
photo = e.getString("photo1");
map.put("id", String.valueOf(i));
map.put("percent", e.getString("percentage_depreciation_value")+"%");
map.put("propertyid", "#"+e.getString("property_id")+" ");
map.put("cityname",e.getString("city_name")+",");
map.put("statecode",e.getString("state_code"));
map.put("propertytype","| "+ proptype);
map.put("footage", e.getString("house_square_footage")+" Sq.Ft");
map.put("bathroom", "| "+e.getString("bathrooms")+" Bath, ");
map.put("bedroom", e.getString("bathrooms")+" Bedrooms");
map.put("price", "List Price: $"+e.getString("property_price"));
map.put("estimated", "Base Market Value: "+estimate);
//map.put("photos",photo );
mylist.add(map);
}
}catch(JSONException e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
try
{
aURL = new URL(photo);
}
catch (MalformedURLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection conn = null;
try
{
conn = aURL.openConnection();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
conn.connect();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStream is = null;
try
{
is = conn.getInputStream();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedInputStream bis = new
BufferedInputStream(is,8*1024);
Bitmap bm = BitmapFactory.decodeStream(bis);
//imagemap.put("im",bm);
// mylist1.add(imagemap);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4,
new String[] { "percent","propertyid", "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated" },
new int[] { R.id.percent, R.id.property_id, R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Propertylist.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon: //Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
displaylogin();
break;
case R.id.text: //Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
displayproperty();
break;
// case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
// break;
}
return true;
}
private void displaylogin() {
startActivity(new Intent(this,Changezip.class));
finish();
}
private void displayproperty() {
startActivity(new Intent(this,property.class));
}
}
I want to put bitmap images into ImageView of ListView. i changed image from url to bitmap image. I have 10 images and i have to put the images in each item of the ListView. Is there any method other than Lazy Adapter ?? Thanks in advance!!!
this is my code
public class Propertylist extends ListActivity {
String proptype;
String prop;
String estimate;
String photo;
String get;
String[] data;
TextView text;
URL aURL;
InputStream is = null;
String result = "";
JSONObject jArray = null;
//Hashtable<String,Bitmap> imagemap;
private ArrayList<NameValuePair> nameValuePairs;
private LayoutInflater inflater;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
text = (TextView) findViewById(R.id.text);
Toast.makeText(getApplicationContext(), "Displaying popertylist for zipcode "+get, Toast.LENGTH_LONG).show();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//ArrayList<Hashtable<String, Bitmap>> mylist1 = new ArrayList<Hashtable<String, Bitmap>>();
Bundle bundle = this.getIntent().getExtras();
get = bundle.getString("name");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.propertyhookup.com/mobile/propertylist.php");
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("zipcode", get.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
if(result.length()<= 7){
Toast.makeText(getApplicationContext(), "No properties for this zipcode or check your zipcode ", Toast.LENGTH_LONG).show();
text.setText("No properties for this zipcode or check your zipcode");
}
else{
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
//JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.1.111/propertyhookup.com/mobile/propertylist.php");
try{
JSONArray earthquakes = jArray.getJSONArray("earthquakes");
for(int i=0;i<10;i++){
HashMap<String, String> map = new HashMap<String, String>();
//imagemap = new Hashtable<String, Bitmap>();
JSONObject e = earthquakes.getJSONObject(i);
if(e.getString("property_type").contains("1")) {
proptype ="Single Family Home";
}else if(e.getString("property_type").contains("2")) {
proptype="Condo";
}else if(e.getString("property_type").contains("3")) {
proptype="Townhouse";
}
if(e.getString("estimated_price").contains("0")) {
estimate = "Not Enough Market Value";
//estimat = (TextView) findViewById(R.id.estimat);
//estimat.setTextColor(Color.rgb(0, 0, 23));
}else {
estimate = "$"+e.getString("estimated_price");
}
photo = e.getString("photo1");
map.put("id", String.valueOf(i));
map.put("percent", e.getString("percentage_depreciation_value")+"%");
map.put("propertyid", "#"+e.getString("property_id")+" ");
map.put("cityname",e.getString("city_name")+",");
map.put("statecode",e.getString("state_code"));
map.put("propertytype","| "+ proptype);
map.put("footage", e.getString("house_square_footage")+" Sq.Ft");
map.put("bathroom", "| "+e.getString("bathrooms")+" Bath, ");
map.put("bedroom", e.getString("bathrooms")+" Bedrooms");
map.put("price", "List Price: $"+e.getString("property_price"));
map.put("estimated", "Base Market Value: "+estimate);
//map.put("photos",photo );
mylist.add(map);
}
}catch(JSONException e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
try
{
aURL = new URL(photo);
}
catch (MalformedURLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection conn = null;
try
{
conn = aURL.openConnection();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
conn.connect();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStream is = null;
try
{
is = conn.getInputStream();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedInputStream bis = new
BufferedInputStream(is,8*1024);
Bitmap bm = BitmapFactory.decodeStream(bis);
//imagemap.put("im",bm);
// mylist1.add(imagemap);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4,
new String[] { "percent","propertyid", "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated" },
new int[] { R.id.percent, R.id.property_id, R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Propertylist.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon: //Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
displaylogin();
break;
case R.id.text: //Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
displayproperty();
break;
// case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
// break;
}
return true;
}
private void displaylogin() {
startActivity(new Intent(this,Changezip.class));
finish();
}
private void displayproperty() {
startActivity(new Intent(this,property.class));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想查看 GreenDroid 库;它让做这样的事情变得微不足道。
请注意,您之前曾提出过 4 个已收到答复的问题,但您尚未接受其中任何一个。该社区的运作基于人们接受答案,如果您不开始接受您认为有用的答案,您可能会发现人们不再回答您。
You might want to check out the GreenDroid library; it makes doing things like this trivial.
Please note that you have asked 4 previous questions that received answers and you have not accepted any of them. This community functions based on people accepting answers and if you don't start accepting answers you've found useful, you may find that people stop answering you.