我尝试使用Custom ListView扩展BaseAdapter将数据库mysql中的数据显示到listview。数据库中的数据如:
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
我的问题是为什么列表视图显示从id 1到6开始的重复项目以及数据id 7,8,9,10未在列表视图中显示。
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 |
BaseAdapter:
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
}
}
}
}
请帮忙。谢谢你提前
答案 0 :(得分:3)
您的getView
实施不正确。试试这个 -
@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;
}
答案 1 :(得分:2)
我认为您没有将else
添加到为convertView指定的条件为null。
所以现在你的代码只为那些当前正在显示的视图设置数据,对于剩余的数据,它重复先前已经初始化的视图。
试试这样,我相信你会得到解决方案:
if(convertView == null){
// some code here
}
else{
holder = (ViewHolder) row.getTag();
}
答案 2 :(得分:0)
我认为getItem()应该返回userlist.get(position)和onCreateView
map = membersList.get(position);
应该是
ap = userlist.get(position);
另一件事是转换视图不为null时缺少else子句。
答案 3 :(得分: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);
}else{
LayoutInflater layoutInflator = convertView;
}
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;
}
答案 4 :(得分:0)
您尚未正确实施Base适配器。 android出于性能原因回收视图,因此您需要初始化文本图像等。 everytime。所以你只需要将initiliazation代码放在convertview null检查之外。 只需这样做: -
@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);
tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
}
map = membersList.get(position);
tvUsername.setText(map.get(FragmentHome.USERNAME));
tvTitle.setText(map.get(FragmentHome.TITLE));
return convertView;
}
答案 5 :(得分:0)
public View getView(int position,View view,ViewGroup parent){ // TODO自动生成的方法存根
$("#hide").removeClass('hide');
检查此代码以了解何时回到父活动并通过.clear()方法清除数据。我在我的应用程序及其工作中使用了此代码,因此您可以从此代码中了解如何管理列表视图中的重复条目
答案 6 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
} else {
gridView = (View) convertView;
}
gridView = inflater.inflate(R.layout.worker_listmain, null);
// your source code here!!! Run 100%
return gridView;
}
答案 7 :(得分:0)
我认为您必须在活动类的清单文件中添加此行。 机器人:windowSoftInputMode = “adjustPan”