Reputation: 48
I try to show data from database mysql to listview with Custom ListView extends BaseAdapter. Data in database like :
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
My Question is Why listview showing duplicate item starting from id 1 to 6 and data id 7, 8, 9, 10 not shown at listview.
Data Shown at Listview like :
| 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;
Fragment
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
}
}
}
}
Please help. Thanks for advance
Upvotes: 1
Views: 3841
Reputation: 203
i think you should have to add this line in your manifests file in your activity class. android:windowSoftInputMode="adjustPan"
Upvotes: 0
Reputation: 257
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;
}
Upvotes: 0
Reputation:
public View getView(int position, View view, ViewGroup parent) { // TODO Auto-generated method stub
// get the layout inflater
LayoutInflater mLayoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// create a ViewHolder reference
ViewHolder holder = null;
// check to see if the reused view is null or not, if is not
// null then reuse it
if (view == null) {
holder = new ViewHolder();
// check to see if the reused view is null or not, if is not
// null then reuse it
if (view == null) {
holder = new ViewHolder();
view = mLayoutInflater.inflate(
R.layout.listview_values, null);
holder.txt_date = (TextView) view
.findViewById(R.id.t_date);
holder.txt_intime = (TextView) view
.findViewById(R.id.txt_intime);
holder.txt_outtime = (TextView) view
.findViewById(R.id.txt_outtime);
// the setTag is used to store the data within this view
view.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag
// to the view
holder = (ViewHolder) view.getTag();
view = mLayoutInflater.inflate(
R.layout.listview_values, parent, false);
}
}
// get the string item from the position "position" from array
// list to put it on the TextView
String date = attendence_webdata.get(position).get("A_date")
.toString();
String intime = attendence_webdata.get(position)
.get("Ain_time").toString();
String outtime = attendence_webdata.get(position)
.get("out_time").toString();
if (date != null) {
if (holder.txt_date != null) {
// set the item name on the TextView
holder.txt_date.setText(date);
}
}
if (intime != null) {
if (holder.txt_intime != null) {
// set the item name on the TextView
holder.txt_intime.setText(intime);
}
}
if (outtime != null) {
if (holder.txt_outtime != null) {
// set the item name on the TextView
holder.txt_outtime.setText(outtime);
}
}
// this method must return the view corresponding to the data at
// the specified position.
return view;
}
}
}
/**
* Static class used to avoid the calling of "findViewById" every time the
* getView() method is called, because this can impact to your application
* performance when your list is too big. The class is static so it cache
* all the things inside once it's created.
*/
private static class ViewHolder {
private TextView txt_outtime;
private TextView txt_intime;
private TextView txt_date;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
attendence_webdata.clear();// where attendence_webdata is arraylistname
list.setAdapter(null); //where list is your listview name
}
check this code for when u back to parent activity and clear your data by .clear() method. i used this code in my app and its working so u can get idea from this code how can manage duplicate entries in list view
Upvotes: 0
Reputation: 8614
Your getView
implementation is not correct. Try this -
@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;
}
Upvotes: 3
Reputation: 3513
You have not implemented your Base adapter correctly. android for performance reason recycle the views so you need to initialize your text image,etc. everytime.So you just need to put the initiliazation code outside the convertview null check. Just do this:--
@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;
}
Upvotes: 0
Reputation: 4948
try this:
@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;
}
Upvotes: 0
Reputation: 486
I think you did not add else
to the condition specified for convertView as null.
So right now your code just setting data only for those views which are currently displaying, for the remaining data it repeats the view that have been previously initialized.
Just try like this, i m sure u'll get the solution:
if(convertView == null){
// some code here
}
else{
holder = (ViewHolder) row.getTag();
}
Upvotes: 2
Reputation: 1268
I think getItem() should return userlist.get(position) and in onCreateView
map = membersList.get(position);
should be
ap = userlist.get(position);
Another thing is the else clause missing when convert view is not null.
Upvotes: 0