簡體   English   中英

ListView 僅顯示 1 個數據,而列表視圖適配器中有 3 個記錄

[英]ListView is showing only 1 data while there are 3 records coming in the listview adapter

我想要一個 tabhost 中的列表視圖。 我的數據來自數據庫。 通過改造,我從數據庫中獲得了 3 條記錄。 我將這 3 條記錄傳遞給我創建的 ListView Adapter。 這些記錄一直到 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
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate(getPostion()))); 
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));

這可能有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM