- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是新的 android 现在我想显示来自 url 的图像。我在 ListView 中使用 ImageView 。我想将图像列表添加到列表项的每一行中。我使用了 SimpleAdapter,但 imageview 显示空白。
这是主要 Activity :
package com.example.mysqltest;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
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.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ReadComments extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// testing on Emulator:
private static final String READ_COMMENTS_URL = "http://192.168.30.198/test/webservice/comments.php";
// JSON IDS:
private static final String TAG_SUCCESS = "success";
private static final String TAG_TITLE = "title";
private static final String TAG_POSTS = "posts";
private static final String TAG_POST_ID = "post_id";
private static final String TAG_USERNAME = "username";
private static final String TAG_MESSAGE = "message";
private static final String TAG_IMAGE = "image";
// An array of all of our comments
private JSONArray mComments = null;
// manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.read_comments);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
}
public void addComment(View v) {
Intent i = new Intent(ReadComments.this, AddComment.class);
startActivity(i);
}
/**
* Retrieves recent post data from the server.
*/
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String username = c.getString(TAG_USERNAME);
String image = c.getString(TAG_IMAGE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_USERNAME, username);
map.put(TAG_IMAGE, image);
// adding HashList to ArrayList
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME,TAG_IMAGE }, new int[] { R.id.title, R.id.message,
R.id.username, R.id.imageView1 });
// I shouldn't have to comment on this one:
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadComments.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
最佳答案
好的。所以我在这里假设您坚持使用 SimpleAdapter
。没有问题,问题已解决,只需按照以下步骤操作:
首先让我们为我们的 ListView
创建我们的自定义 row.xml,它只包含一个 ImageView
。(你可以添加任何你喜欢的稍后再使用,但现在我假设您只想加载 ImageView
)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
其次让我们创建我们的自定义 SimpleAdapter
package com.example.helpstack;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
public class MySimpleAdapter extends SimpleAdapter {
private Context mContext;
public LayoutInflater inflater = null;
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
mContext = context;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.row, null);
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
new DownloadTask((ImageView) vi.findViewById(R.id.imageView1))
.execute((String) data.get("uri"));
return vi;
}
}
第三,让我们创建我们的 DownloadTask
,这个类将下载图像:
package com.example.helpstack;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class DownloadTask extends AsyncTask<String, Void, Boolean> {
ImageView v;
String url;
Bitmap bm;
public DownloadTask(ImageView v) {
this.v = v;
}
@Override
protected Boolean doInBackground(String... params) {
url = params[0];
bm = loadBitmap(url);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
v.setImageBitmap(bm);
}
public static Bitmap loadBitmap(String url) {
try {
URL newurl = new URL(url);
Bitmap b = BitmapFactory.decodeStream(newurl.openConnection()
.getInputStream());
return b;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
现在 DownloadTask
正在 SimpleAdapter
的 getView() 内部使用
第四步让我们从 MainActivity.java
运行我们惊人的小项目package com.example.helpstack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("uri",
"http://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png");
//here u can add as many uri as u want
data.add(map);
MySimpleAdapter adapter = new MySimpleAdapter(MainActivity.this, data,
R.layout.row, new String[] {}, new int[] {});
lv.setAdapter(adapter);
}
}
关于Android:通过 SimpleAdapter 在 imageview 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23985786/
这是我的适配器 public class Vk_row_adapter extends SimpleAdapter { final String ATTRIBUTE_NAME_TEXT_NAME =
行中的空指针异常: listView.setAdapter(sAdapter); public class MyEmployeFragment extends Fragment { M
我想填写我的 ListView一些数据,我使用 SimpleAdapter .但是我觉得SimpleAdapter仅适用于 List>我有 List喜欢遵循代码: 我能做什么?有什么办法吗? List
代码编译没有错误,但结果在我的手机上只是一个空列表。那么问题是什么?这是我在 Activity 中的 onCreate 方法 super.onCreate(savedInstanceState
我使用了两个不同颜色的自定义行。但它返回同一行黑色。我也有一个黄色行,但它只返回一个黑色行。我无法理解它,因此我在下面分享了我的源代码。请帮助我 json_orders = json.getJSONA
我有一个带有过滤 inputSearch 和可点击项(我从 MySQL 数据库中获取)的 SimpleAdapter。 有时当我吃午饭时我的项目会出现错误: java.lang.RuntimeExce
我有一个与自定义 SimpleAdapter 关联的 ListView 。我制作了自定义 SimpleAdapter,因为我想将不同的事件处理程序与包含的 TextView 和 ImageView 相
我是 Android 开发新手,我花了几天时间尝试让我的应用程序运行,但没有成功。我正在使用 AsyncTask 和 SimpleAdapter,它返回数据,但是我的 JSON 有子项。它是注释的 J
我向 SQLite 添加了许多数据,但我的 ListView 仅显示最后插入的数据。所有行都有相同的值。 public void onResume() { super.onRe
我想根据应用程序中的某些值对我的 Listview 进行排序,但我不知道如何编码。我研究发现人们正在使用比较器和集合来对数组列表进行排序。基本上,我想按某些状态进行排序,例如接受或拒绝。该值存储在 T
我正在使用此代码创建一个 SimpleAdapter String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_CHECKED, ATTRIBUT
我正在开发一个应用程序,可以在 reomte 数据库上进行查询,使用 Json 和 PHP 通过 MySQL 稍后将结束 posgress,因为它更加开放。 我的问题是,我的 SimpleAdapte
当我运行程序时,每行中的 TextView 文本都从默认文本更改为无。 HashMap 中的数据未显示。 我的主要应用布局(main.xml): 我的列表项布局(myrow
我有一个 SimpleAdapter,我用它来显示两行 ListView 。我希望这个简单适配器上的第 15 个位置具有不同的格式,但是当我滚动 ListView 时,它会丢失位置并且格式会更改位置。
我正在尝试在 SET 按钮上填充 ListView 。当我选择值微调器并根据列表进行填充时。我面临的问题是我多次点击 SET 按钮,它会将项目添加到 ListView 。 setButton.s
我用 json 创建 Rss,当我从后台获取 jsonObject 时,在 postExecute 中我用对象结果创建 Map,然后出现错误LogCat 导致 SimpleAdapter 中的 Web
在我的应用程序中,我试图在单击列表项时将 TextView 设置为删除线。但是 Listview clik 项目不起作用。 代码如下: listView = ( ListView ) find
我正在从 web.xml 文件和图像中读取数据。我将图像作为 Bitmap bmImg 获取,并将其放入 Hashmap 中。但我确实知道如何修改我的 simpleAdapter 函数以在 image
我目前正在使用联系人的 ContentProvider 从设备中检索联系人,并允许用户通过在 EditText 中键入来过滤结果。 为此,我在 SimpleAdapter 上设置了一个过滤器查询,如下
我使用 SAXparser 从各种提要中获取数据,然后使用 SimpleListAdapter 将其添加到单个列表中, 我使用以下代码将 RSS Feed 添加到我的 SimpleListAdapte
我是一名优秀的程序员,十分优秀!