我希望在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
答案 0 :(得分:0)
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate(getPostion())));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
这可能有效。