Reputation: 24184
Problem Statement:-
In my below code, I am getting list of User's
which I am showing currently on the google map
which is working fine for me. And also I need to show them on the ListView
which is not working correctly.
So currently I have list of two user's in the response
. And if I try to print them in the for loop
, I can see both of the user getting printed correctly. But when I try to add them in my ListView
, I see only one user
getting shown on the ListView twice
? But I should be seeing both the user one after another on the ListView
.
@Override
protected void onPostExecute(ArrayList<User> response) {
for(User user : response){
System.out.println("User:- " +user.getFirstName());
// Something wrong I am doing in below line
adapter=new LazyAdapter(ThesisProjectAndroid.this, mStrings, user);
if(user.getGender()==1) {
Drawable marker=getResources().getDrawable(R.drawable.current_user);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, 0, markerHeight, markerWidth);
myItemizedOverlay = new MyItemizedOverlay(marker);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint((int) (user.getLatitude()*1000000), (int) (user.getLongitude()*1000000));
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
} else {
Drawable marker1=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth1 = marker1.getIntrinsicWidth();
int markerHeight1 = marker1.getIntrinsicHeight();
marker1.setBounds(0, 0, markerHeight1, markerWidth1);
myItemizedOverlay = new MyItemizedOverlay(marker1);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint2 = new GeoPoint((int) (user.getLatitude()*1000000), (int) (user.getLongitude()*1000000));
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
}
listView.setAdapter(adapter);
}
And this is my LazyAdapter class
.
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private User userList;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public LazyAdapter(Activity b, String[] mStrings, User user) {
activity = b;
data=mStrings;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
userList = user;
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);
TextView text1=(TextView)vi.findViewById(R.id.text1);
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText("Name: " +userList.getFirstName()+" "+userList.getLastName());
text1.setText("Gender: " +userList.getGender());
imageLoader.DisplayImage(data[position], image);
return vi;
}
}
And this is the XML for showing user's list
- I need to show User's Full Name
on one TextView
and Gender
on second TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip" android:src="@drawable/stub" android:scaleType="centerCrop"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>
And this is what I am getting in my Emulator- One User showing twice in my listview why is it so? But it should be One user information
at the top
and second user information
below that.
Upvotes: 0
Views: 97
Reputation: 63955
You need to provide the complete list of users to your adapter
@Override
protected void onPostExecute(ArrayList<User> response) {
// add all users to the adapter
adapter=new LazyAdapter(ThesisProjectAndroid.this, mStrings, response);
for(User user : response){
System.out.println("User:- " +user.getFirstName());
// ---- here be ur other code ----
}
listView.setAdapter(adapter);
}
Your Adapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private List<User> userList;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public LazyAdapter(Activity b, String[] mStrings, List<User> user) {
activity = b;
data=mStrings;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
userList = user;
}
// ---- here be ur other code ----
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);
TextView text1=(TextView)vi.findViewById(R.id.text1);
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText("Name: " +userList.get(position).getFirstName()+" "+userList.get(position).getLastName());
text1.setText("Gender: " +userList.get(position).getGender());
imageLoader.DisplayImage(data[position], image);
return vi;
}
}
Upvotes: 2