【发布时间】:2014-07-17 12:21:29
【问题描述】:
我尝试使用自定义 ListView 扩展 BaseAdapter 将数据从数据库 mysql 显示到列表视图。数据库中的数据如:
tbl_userbook:
id | username | title | category
------+-----------+-----------------+-------------
1 | A | Java | 1
2 | B | VB.NET | 1
3 | C | Swing Java | 1
4 | D | Java Hibernate | 1
5 | E | C# | 1
6 | F | Ruby | 1
7 | G | MySQL | 1
8 | H | Sqlite | 1
9 | I | PHP | 1
10 | J | MsSQL | 1
我的问题是为什么 listview 显示从 id 1 到 6 的重复项和数据 id 7、8、9、10 未显示在 listview 中。
Listview 中显示的数据如下:
| username | title |
+-----------+-----------------+
| A | Java |
| B | VB.NET |
| C | Swing Java |
| D | Java Hibernate |
| E | C# |
| F | Ruby |
| A | Java | < ---- duplicate starting from here
| B | VB.NET |
| C | Swing Java |
| D | Java Hibernate |
基础适配器:
public class HomeListViewAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String, String>> userList;
HashMap<String, String> map = new HashMap<String, String>();
public HomeListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
userList = arraylist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tvUsername, tvTitle;
if (convertView == null) {
LayoutInflater layoutInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflator.inflate(R.layout.custom_listview_home, parent, false);
map = membersList.get(position);
tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
tvUsername.setText(map.get(FragmentHome.USERNAME));
tvTitle.setText(map.get(FragmentHome.TITLE));
}
return convertView;
片段
public class FragmentHome extends SherlockFragment {
public static String USERNAME = "username";
public static String TITLE = "title";
private ProgressDialog pDialog;
JSONPostGet jPostget = new JSONPostGet();
JSONArray users = null;
ArrayList<HashMap<String, String>> userList;
private static String url_getbook = "http://....../get_userbook.php";
ListView list;
HomeListViewAdapter homeadapter;
private static final String KEY_SUCCESS = "success";
private static final String KEY_USERS = "users";
private static final String KEY_USERNAME = "username";
private static final String KEY_TITLE = "title";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
userList = new ArrayList<HashMap<String,String>>();
new LoadUserBook().execute();
return rootView;
}
class LoadUserBook extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("category", "1"));
JSONObject json = jPostget.makeHttpRequest(url_getbook, "GET", params);
Log.d("All News Feed: ", json.toString());
try {
int success = json.getInt(KEY_SUCCESS);
if (success == 1) {
users = json.getJSONArray(KEY_USERS);
for (int i = 0; i < users.length(); i++) {
JSONObject c = users.getJSONObject(i);
String suname = c.getString(KEY_USERNAME);
String stitle = c.getString(KEY_TITLE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, suname);
map.put(KEY_TITLE, stitle);
userList.add(map);
}
} else {
// ...
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
pDialog.dismiss();
try {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
list = (ListView) getView().findViewById(R.id.listview1);
homeadapter = new HomeListViewAdapter(getActivity(), userList);
list.setAdapter(homeadapter);
homeadapter.notifyDataSetChanged();
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
请帮忙。感谢提前
【问题讨论】:
-
在 doInBackground() 方法中使用 userList.clear() ,例如 . @Override protected String doInBackground(String... args) { userList.clear()
-
哈哈。我只是删除它“ if (convertView == null) ”问题解决了,但请你给我一个为什么会发生这种情况的原因。
-
你不应该删除它..它有助于保持顺利
-
如果在 doInBackground() 中使用,我必须将代码“userList.clear()”放在哪里,数据不显示?
-
@eric : 那我该怎么办?
标签: android mysql listview android-listview baseadapter