Я новичок в Android Studio. Моя цель — загрузить свои данные с сервера Wamp, используя PHP с MySQL, в recyclerView, используя CardView. Но все работает нормально. Но я не получаю вывод, когда я запускаю свои приложения.
Моя зависимость в этом, пожалуйста, игнорируйте Firebase
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 class
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;
}
}
тогда мой макет
План деятельности с 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 Layout
<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
Других решений пока нет …