【问题标题】:ListView is showing only 1 data while there are 3 records coming in the listview adapterListView 仅显示 1 条数据,而 listview 适配器中有 3 条记录
【发布时间】:2019-01-29 06:27:20
【问题描述】:

我想要一个 tabhost 中的列表视图。我的数据来自数据库。通过改造,我从数据库中获得了 3 条记录。我将这 3 条记录传递给我创建的 ListView 适配器。这些记录一直到 Adapter 的构造函数,但之后在 getView 方法中只有 1 条记录被访问 3 次。我不确定为什么会这样。

这是我的帖子活动:

public  static final ArrayList<WorkProfilePojo> mProfiles = new ArrayList<>();
BaseURL baseURL = new BaseURL();
VendorPostAdapter pAdapter;
ListView mPostList;
public List<WorkProfilePojo> returnedList = new ArrayList<>();

String lv_vendorId = null;
public static String lv_name;

@Override
protected void onCreate(Bundle savedInstanceState) {

    Intent intent = getIntent();
    lv_vendorId = intent.getStringExtra("lv_vendorId");
    Log.e("vendor id", lv_vendorId);
    lv_name = intent.getStringExtra("lv_name");


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    getRetrofit();

}

private void getRetrofit() {
    Retrofit retrofit = new RetrofitObject().getRetrofit();

    final WorkProfileforPostTabAPI mProfileAPI = 
retrofit.create(WorkProfileforPostTabAPI.class);
    final Call<ArrayList<WorkProfilePojo>> mCall = 
mProfileAPI.getWork(lv_vendorId);
    mCall.enqueue(new Callback<ArrayList<WorkProfilePojo>>() {
        @Override
        public void onResponse(Call<ArrayList<WorkProfilePojo>> call, 
Response<ArrayList<WorkProfilePojo>> response) {
            mProfiles.clear();
            returnedList = (ArrayList<WorkProfilePojo>)response.body();
            WorkProfilePojo wp;
            Log.e("Teste2", 
returnedList.get(0).getLv_eventSubCategory());
            for (int i = 0; i<= returnedList.size()-1; i++){
                wp=new WorkProfilePojo();


wp.setLv_vendorWorkId(returnedList.get(i).getLv_vendorWorkId());

wp.setLv_eventSubCategory(returnedList.get(i).getLv_eventSubCategory());

wp.setLv_workDescription(returnedList.get(i).getLv_workDescription());

wp.setLv_numWorkLikes(returnedList.get(i).getLv_numWorkLikes());

wp.setLv_numWorkComments(returnedList.get(i).getLv_numWorkComments());

                mProfiles.add(wp);
            Log.e("retrofit profile size: ", 
String.valueOf(mProfiles.size()));
            populateListView(mProfiles);
        }

        @Override
        public void onFailure(Call<ArrayList<WorkProfilePojo>> call, 
Throwable t) {
            Log.e(TAG, "FAIL");
        }

    });
}

private void populateListView(ArrayList<WorkProfilePojo> mProfiles) {
    mPostList               = (ListView) findViewById(R.id.listVPost);

    Log.e("func prof size: ", String.valueOf(mProfiles.size()));
    pAdapter = new VendorPostAdapter(this, mProfiles, lv_name);

    mPostList.setAdapter(pAdapter);

}

这是我的适配器:

public class VendorPostAdapter extends BaseAdapter {

Context context;
ArrayList<WorkProfilePojo> lv_profiles = new ArrayList<>();
String lv_name;
LayoutInflater inflater;

public VendorPostAdapter(Context context, ArrayList<WorkProfilePojo> 
lv_profiles, String lv_name){
    this.context = context;
    this.lv_profiles =lv_profiles;
    this.lv_name = lv_name;
    Log.e("adapter name", lv_name );
    Log.e("adapter workid", lv_profiles.get(0).getLv_vendorWorkId());
    Log.e("adapter workid", lv_profiles.get(1).getLv_vendorWorkId());
    Log.e("adapter workid", lv_profiles.get(2).getLv_vendorWorkId());
}

private class ViewHolder {
    TextView mtxtViewPartnerName;

    TextView mtxtViewEventCategory;
    TextView mtxtViewDate         ;
    TextView mtxtViewFillDescription;
    GridView mgrdViewPhotos        ;
    ImageView mimgLike             ;
    ImageView mimgPostProfilePic   ;
    ImageView mimgShare            ;
    ImageView mimgComment          ;

    ImageView mimgLikeThumb  ;
    TextView mtxtNoOfLikes         ;
    TextView mtxtNoOfComments      ;
    TextView mtxtComments;
}

@Override
public int getCount() {
    return lv_profiles.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    inflater = (LayoutInflater) 
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.content_post, parent, 
false);
        holder = new ViewHolder();

        holder.mtxtViewPartnerName = (TextView) 
convertView.findViewById(R.id.txtViewPartnerName);

        holder.mtxtViewEventCategory= (TextView) 
convertView.findViewById(R.id.txtViewEventCategory);
        holder.mtxtViewDate            = (TextView) 
convertView.findViewById(R.id.txtViewDate);
        holder.mtxtViewFillDescription = (TextView)     
convertView.findViewById(R.id.txtViewFillDescription);
        holder.mgrdViewPhotos          = (GridView) 
convertView.findViewById(R.id.grdViewPhotos);
        holder.mimgLike                = (ImageView) 
convertView.findViewById(R.id.imgLike);
        holder.mimgPostProfilePic      = (ImageView) 
convertView.findViewById(R.id.imgPostProfilePic);
        holder.mimgShare               = (ImageView) 
convertView.findViewById(R.id.imgShare);
        holder.mimgLikeThumb           = (ImageView)     
convertView.findViewById(R.id.imgLikeThumb);
        holder.mimgComment             = (ImageView) 
convertView.findViewById(R.id.imgComment);
        holder.mtxtNoOfLikes            = (TextView) 
convertView.findViewById(R.id.txtViewNoOfLikes);
        holder.mtxtNoOfComments         = (TextView) 
convertView.findViewById(R.id.txtViewNoOfComments);
        holder.mtxtComments             = (TextView) 
convertView.findViewById(R.id.txtViewComments);

        convertView.setTag(holder);

    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    final WorkProfilePojo wp = lv_profiles.get(position);

    Log.e("getView name", lv_name );
    Log.e("getView workid", 
lv_profiles.get(position).getLv_vendorWorkId());
    holder.mtxtViewPartnerName.setText( lv_name );
    holder.mtxtViewEventCategory.setText( wp.getLv_eventSubCategory() );

    FormatDate lv_date = new FormatDate();
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate()));
    holder.mtxtViewFillDescription.setText(wp.getLv_workDescription());
    holder.mtxtViewFillDescription.setText(wp.getLv_workDescription());

    holder.mimgLikeThumb.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
            Intent intent = new Intent(context, 
VendorWorkLikesActivity.class);
            intent.putExtra("lv_workId", wp.getLv_vendorWorkId());
            Log.e("postad workid", wp.getLv_vendorWorkId());
            context.startActivity(intent);
        }
    });

    holder.mtxtComments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, 
VendorWorkCommentActivity.class);
            context.startActivity(intent);
        }
    });
    return convertView;
}
}

这是我的activity_post.xml 包裹在相对布局中

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundcolour"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="1"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    />
</RelativeLayout>

这是我的 content_post,用于包裹在相对布局中的订单项:

<RelativeLayout
    android:layout_marginTop="20dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgPostProfilePic"
        android:src="@drawable/profileicon"
        android:layout_width="60dp"
        android:layout_height="60dp" />

    <TextView
        android:id="@+id/txtViewPartnerName"
        style="@style/InputLable"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_toEndOf="@id/imgPostProfilePic"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="partner Name" />

    <TextView
        android:id="@+id/txtViewManageWork"
        style="@style/Keywords"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="5dp"
        android:text="Managed" />

    <TextView
        android:id="@+id/txtViewEventCategory"
        style="@style/InputLable"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginLeft="220dp"
        android:paddingRight="5dp"
        android:layout_marginTop="5dp"
        android:text="" />

    <TextView
        android:id="@+id/txtViewEvent"
        style="@style/Keywords"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_toEndOf="@id/txtViewEventCategory"
        android:layout_marginTop="5dp"
        android:text="Event" />


    <TextView
        android:id="@+id/txtViewDate"
        style="@style/InputLable"
        android:layout_width="100dp"
        android:layout_height="25dp"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="30dp"
        android:text="Date" />

    <TextView
        android:id="@+id/txtViewDescription"
        style="@style/Keywords"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="80dp"
        android:text="Work Description" />

    <TextView
        android:id="@+id/txtViewFillDescription"
        style="@style/InputLable"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="100dp"
        android:text="XYZ" />

    <TextView
        android:id="@+id/txtViewPhotos"
        style="@style/Keywords"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="130dp"
        android:text="Photos" />

    <GridView
        android:id="@+id/grdViewPhotos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:numColumns="auto_fit"
        android:layout_below="@id/txtViewPhotos">

    </GridView>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:id="@+id/divider"
        android:background="@color/colorDarkGray"
        android:layout_below="@id/grdViewPhotos"
        android:layout_weight="0"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dividerlayout"
        android:orientation="horizontal"
        android:layout_below="@id/divider"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageView
            android:id="@+id/imgLikeThumb"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="5dp"
            android:layout_below="@id/grdViewPhotos"
            android:clickable="true"
            android:src="@drawable/likethumb" />

        <TextView
            android:id="@+id/txtViewNoOfLikes"
            style="@style/InputLable"
            android:layout_width="30dp"
            android:layout_height="25dp"
            android:layout_alignParentEnd="true"
            android:textAlignment="center"
            android:layout_marginTop="5dp"
            android:text="0" />



        <TextView
            android:id="@+id/txtViewNoOfComments"
            style="@style/InputLable"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="5dp"
            android:textAlignment="center"
            android:text="0" />

        <TextView
            android:id="@+id/txtViewComments"
            style="@style/InputLable"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:text="Comments" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:id="@+id/divider1"
        android:background="@color/colorDarkGray"
        android:layout_below="@id/dividerlayout"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="0"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dividerlayout1"
        android:layout_below="@id/dividerlayout"
        android:layout_marginTop="5dp">

    <ImageView
        android:id="@+id/imgLike"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_below="@id/divider1"
        android:layout_marginTop="5dp"
        android:clickable="true"
        android:src="@drawable/likeicon2" />

        <TextView
            android:id="@+id/txtViewLike"
            style="@style/InputLable"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@id/divider1"
            android:text="Likes" />

    <ImageView
        android:id="@+id/imgShare"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="60dp"
        android:layout_below="@id/divider1"
        android:layout_centerInParent="true"
        android:layout_marginTop="5dp"
        android:clickable="true"
        android:src="@drawable/shareicon3" />

        <TextView
            android:id="@+id/txtViewShare"
            style="@style/InputLable"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@id/divider1"
            android:text="Share" />

    <ImageView
        android:id="@+id/imgComment"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="45dp"
        android:layout_below="@id/divider1"
        android:layout_marginTop="5dp"
        android:clickable="true"
        android:src="@drawable/commenticon" />

        <TextView
            android:id="@+id/txtViewComment"
            style="@style/InputLable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@id/divider1"
            android:text="Comment" />

    </LinearLayout>

</RelativeLayout>


From retrofit results, I am getting 3 records from database:
func workid:: W00000000000013
func workid:: W00000000000014
func workid:: W00000000000015

But in getView() method I am getting only 1 record coming 3 times:
Likesad name: W00000000000013
Likesad name: W00000000000013
Likesad name: W00000000000013

【问题讨论】:

  • 你好首先删除 populateListView(mProfiles);从循环并将其放在 for 循环之外,您是否在适配器项目布局填充中使用了 GridView 视图?尝试滚动您的列表视图可能是其他项目,并且一次只能显示一个项目
  • 我没有看到你的 for 循环的右括号,请先更新你的代码。
  • public static final ArrayList mProfiles = new ArrayList();删除决赛。还删除 populateListView(mProfiles);从循环。
  • populateListView(mProfiles);仅在 for 循环之外。不知道为什么没有出现右括号。
  • 尝试删除 final。但它不接受 holder.mimgLikeThumb.setOnClickListener 方法。在 intent.putExtra("lv_workId", wp.getLv_vendorWorkId()) 中访问 wp 时出错;

标签: android listview android-tabhost


【解决方案1】:
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate(getPostion()))); 
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));

这可能有效。

【讨论】:

  • 它不起作用,因为函数 getLv_creationDate() 是 pojo 类的 getter 方法,它不带任何参数。
猜你喜欢
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多