gpt4 book ai didi

php - 从 PHP/MySQL 本地推送数据时,CardView 不显示在我的 RecycleView 上

转载 作者:行者123 更新时间:2023-11-30 21:59:36 25 4
gpt4 key购买 nike

我是 Android Studio 的初学者。我的目标是使用 CardView 将我的数据从使用 PHP 和 MySQL 的 Wamp 服务器加载到 recyclerView 中。但一切运行良好。但是当我运行我的应用程序时,我没有得到输出。

我的依赖是这个,请忽略Firebase one

 compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'

compile 'com.google.firebase:firebase-core:10.2.4'
compile 'com.google.firebase:firebase-auth:10.2.4'
compile 'com.google.android.gms:play-services-auth:10.2.4'
compile 'com.android.support:design:25.3.1'
compile 'com.google.firebase:firebase-database:10.2.4'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
testCompile 'junit:junit:4.12'

Main2Activity 类

 public class Main2Activity extends AppCompatActivity {

ProgressDialog progressDialog;
List<companyCardDetailsClass> rowListItem;
private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private CompanyCardRecycleViewAdapterClass adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
rowListItem = new ArrayList<>();
recyclerView = (RecyclerView)findViewById(R.id.recycler_view1) ;

gridLayoutManager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);

adapter = new CompanyCardRecycleViewAdapterClass(this, rowListItem);
recyclerView.setAdapter(adapter);
load_data_from_server(0);
}

private void load_data_from_server(int id) {
AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
//send request to server for data
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://192.168.161.2/stamploaddatabase/getMerchantDetails.php").build();
try {
//get response from server in form of JSon array
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
companyCardDetailsClass data = new companyCardDetailsClass(object.getInt("user_id"), object.getString("companyname"), object.getString("location"),
object.getInt("logo"));
rowListItem.add(data);

}

} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}




<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="10dp">



<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/image_company"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:layout_weight="0.25"
/>


</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8px">

<TextView
android:text="Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/name_company"
card_view:textSize="14sp"
android:layout_weight="2.37"
android:textColor="@android:color/holo_red_light" />

<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/address_company"
card_view:textSize="12sp"
android:layout_weight="0.22"
android:layout_below="@+id/name_company"/>
</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout2"
card_view:orientation="horizontal"
android:layout_gravity="right"
android:orientation="horizontal"
>

<Button
android:text="Redeem"
android:layout_width="112dp"
android:layout_height="32dp"
android:id="@+id/btn_redeem1"
android:paddingRight="20dp"
card_view:text="Redeem"
android:layout_weight="0.25"
android:background="@drawable/shape"/>
card_view:background="@color/btn_redeem"/>

<ImageView
android:layout_width="51dp"
android:layout_height="35dp"
android:src="@drawable/ic_like"
android:id="@+id/like"
/>
<ImageView
android:layout_width="51dp"
android:layout_height="35dp"
android:src="@drawable/ic_share"
android:id="@+id/share"
/>


</LinearLayout>

适配器和支架

 public class CompanyCardRecycleViewAdapterClass extends RecyclerView.Adapter<CompanyCardRecycleViewAdapterClass.ViewHolder> {

List<companyCardDetailsClass> itemList;
private Context context;

public CompanyCardRecycleViewAdapterClass(Context context, List<companyCardDetailsClass> itemList ) {
super();
this.itemList = itemList;
this.context = context;


}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.company_card_list, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.CompanyName.setText(itemList.get(position).getCompanyname());
holder.CompanyAddress.setText(itemList.get(position).getLocation());
holder.CompanyProfilePhoto.setImageResource(itemList.get(position).getImageCpny());

}

@Override
public int getItemCount() {
return this.itemList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView CompanyName;
public TextView CompanyAddress;
public ImageView CompanyProfilePhoto;
public ImageView likeImage;
public ImageView shareImage;
public Button RedeemBtn;
public ViewHolder(View view) {
super(view);
CompanyAddress =(TextView)itemView.findViewById(R.id.address_company);
CompanyName = (TextView)itemView.findViewById(R.id.name_company);
CompanyProfilePhoto = (ImageView)itemView.findViewById(R.id.image_company);
likeImage = (ImageView)itemView.findViewById(R.id.like);
shareImage = (ImageView)itemView.findViewById(R.id.share);
RedeemBtn = (Button)itemView.findViewById(R.id.btn_redeem1);
final RecyclerView recyclerView = (RecyclerView)itemView.findViewById(R.id.recycler_view1);
}
}
}

**我的Java类数据**

public class companyCardDetailsClass {
private int id_company;
private String companyname;


private String location;
private int ImageCpny;

public companyCardDetailsClass() {
}

public companyCardDetailsClass(int id_company, String companyname, String location, int imageCpny) {
this.id_company = id_company;
this.companyname = companyname;
ImageCpny = imageCpny;
this.location = location;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public int getIdCompany(){return id_company;}
public void setId_company(int id_company){this.id_company = id_company;}

public String getCompanyname (){
return companyname;
}
public void setCompanyname (String companyname){
this.companyname = companyname;
}



public int getImageCpny(){
return ImageCpny;
}
public void setImageCpny(int ImageCpny){
this.ImageCpny = ImageCpny;

}
}

然后是我的布局activity Layout with recyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/tools"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.drake.stamploadproject.Main2Activity"
tools:showIn="@layout/activity_main2">
<android.support.v7.widget.RecyclerView
android:id = "@+id/recycler_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginTop="5dp">
</android.support.v7.widget.RecyclerView>

cardView 布局

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="10dp">



<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/image_company"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:layout_weight="0.25"
/>


</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8px">

<TextView
android:text="Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/name_company"
card_view:textSize="14sp"
android:layout_weight="2.37"
android:textColor="@android:color/holo_red_light" />

<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/address_company"
card_view:textSize="12sp"
android:layout_weight="0.22"
android:layout_below="@+id/name_company"/>
</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout2"
card_view:orientation="horizontal"
android:layout_gravity="right"
android:orientation="horizontal"
>

<Button
android:text="Redeem"
android:layout_width="112dp"
android:layout_height="32dp"
android:id="@+id/btn_redeem1"
android:paddingRight="20dp"
card_view:text="Redeem"
android:layout_weight="0.25"
android:background="@drawable/shape"/>
card_view:background="@color/btn_redeem"/>

<ImageView
android:layout_width="51dp"
android:layout_height="35dp"
android:src="@drawable/ic_like"
android:id="@+id/like"
/>
<ImageView
android:layout_width="51dp"
android:layout_height="35dp"
android:src="@drawable/ic_share"
android:id="@+id/share"
/>


</LinearLayout>

最佳答案

运行

@Override
protected void onPostExecute(Void aVoid) {
adapter = new CompanyCardRecycleViewAdapterClass(this,rowListItem);
recyclerView.setAdapter(adapter);
}

您在开始时设置了空列表。 notifyDataSetChange 无法按您预期的方式工作。您应该从 AsyncTask 返回数组或更新 Adapter onPostExecute。

您可以在这里找到更优雅的替代方案:https://stackoverflow.com/a/35148283/6159609

关于php - 从 PHP/MySQL 本地推送数据时,CardView 不显示在我的 RecycleView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com