一个接一个地在ListView上显示用户的信息

时间:2012-08-19 20:56:09

标签: java android android-listview

问题陈述: -

在我的下面的代码中,我收到的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;
    }
}

这是用于展示user's list的XML - 我需要在User's Full Name上展示TextView,在第二Gender展示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 informationtop之下second user information

1 个答案:

答案 0 :(得分: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;
    }
}