0

问题陈述:-

在我下面的代码中,我得到list of User's了我目前正在显示的google map对我来说工作正常的。而且我还需要向他们展示ListView哪些工作不正常。

所以目前我有two user's in the response. 如果我尝试在 中打印它们for loop,我可以看到两个用户都被正确打印。但是当我尝试将它们添加到我的 中ListView时,我看到only one user显示在ListView twice? 但我应该在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);
}

这是我的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;
    }
}

这是用于显示的 XML user's list- 我需要User's Full Name在第一时间TextViewGender第二时间显示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>

这就是我在模拟器中得到的结果——一个用户在我的列表视图中显示了两次,为什么会这样?但它应该One user information在那个topsecond user information下面。

4

1 回答 1

2

您需要向适配器提供完整的用户列表

@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);
}

您的适配器

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;
    }
}
于 2012-08-19T21:22:46.707 回答