我一直在制作一个应用程序,它在一个Activity中使用了一个回收者视图,它是如何显示回收者视图的内容的。 这是我的代码
pending_complaint.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_pending_complaints"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
pendig_complaint_data.java
public class pendig_complaint_data {
String comp_id;
String description;
}
pending_comp_adapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Daniel on 6/20/2017.
*/
public class pending_comp_adapter extends RecyclerView.Adapter<pending_comp_adapter.ViewHolder> {
private final LayoutInflater inflater;
List<pendig_complaint_data> data=new ArrayList<pendig_complaint_data>();
private Context context;
public pending_comp_adapter(Context context,List<pendig_complaint_data> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.pending_complaint_custom_row,parent,false);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
pendig_complaint_data current=data.get(position);
holder.comp_id1.setText(current.comp_id);
holder.desc1.setText(current.description);
Log.e("Daniel1",current.comp_id+" "+data.size());
}
@Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView comp_id1;
TextView desc1;
public ViewHolder(View itemView) {
super(itemView);
comp_id1= (TextView) itemView.findViewById(R.id.complaint_id);
desc1=(TextView)itemView.findViewById(R.id.complaint_desc);
}
}
}
pending_complaints.java
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by Daniel on 5/23/2017.
*/
public class pending_complaints extends AppCompatActivity {
private RecyclerView mrecyclerView;
private pending_comp_adapter adapter;
List<pendig_complaint_data> data= new ArrayList<pendig_complaint_data>();
String url="http://192.168.1.186:90/am/pending_complaints.asp?staff_no=";
String respon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pending_complaints);
new get_complaint_data().execute();
}
class get_complaint_data extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
SharedPreferences settings = getSharedPreferences("STAFF_NO", Context.MODE_PRIVATE);
String staff_no = settings.getString("Staff_no", "");
url+=staff_no;
Log.e("Daniel",url);
Toast.makeText(getApplicationContext(),"Downloading",Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... params) {
HttpHandler sh=new HttpHandler();
respon=sh.makeServiceCall(url);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(),"Done",Toast.LENGTH_SHORT).show();
if(respon.toString().equals(""))
{
Toast.makeText(getApplicationContext(),"No Pending Complaints",Toast.LENGTH_SHORT).show();
finish();
}
else
{ Log.e("Daniel",respon);
String s[]=respon.toString().substring(0,respon.length()-1).split("\\|");
for(int i=0;i<s.length;i++)
{
String s1[]=s[i].split("\\*");
pendig_complaint_data current=new pendig_complaint_data();
current.comp_id=s1[0];
;Log.e("Daniel",current.comp_id);
current.description=s1[1];
Log.e("Daniel",current.description);
data.add(current);
}
mrecyclerView= (RecyclerView) findViewById(R.id.recycler_pending_complaints);
mrecyclerView.setHasFixedSize(true);
adapter=new pending_comp_adapter(getApplicationContext(),data);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mrecyclerView.setLayoutManager(linearLayoutManager);
mrecyclerView.setAdapter(adapter);
}
}
}
}
的build.gradle
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
LogCat日志
06-22 10:28:24.209 29457-29457/com.example.daniel.bhelstockmanager E/RecyclerView: No adapter attached; skipping layout
06-22 10:28:24.261 29457-29457/com.example.daniel.bhelstockmanager E/RecyclerView: No adapter attached; skipping layout
06-22 10:28:24.274 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: 249*test|250*test2|251*test3|252*test4|
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: 249
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: test
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: 250
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: test2
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: 251
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: test3
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: 252
06-22 10:28:24.275 29457-29457/com.example.daniel.bhelstockmanager E/Daniel: test4
06-22 10:28:24.322 29457-29457/com.example.daniel.bhelstockmanager E/Daniel1: 249 test
06-22 10:28:24.325 29457-29457/com.example.daniel.bhelstockmanager E/Daniel1: 250 test2
06-22 10:28:24.326 29457-29457/com.example.daniel.bhelstockmanager E/Daniel1: 251 test3
06-22 10:28:24.329 29457-29457/com.example.daniel.bhelstockmanager E/Daniel1: 252 test4