您好,我可以帮助我将数据从数据库显示到RecyclerView。我到处搜索但是徒劳无功。我成功地用ListView和mysql做到了,但是使用RecyclerView我发现了一些困难。
更新
UsersFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_users, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
inputSearch = (EditText) rootview.findViewById(R.id.inputSearch);
listView.setOnItemClickListener(this);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
((SimpleAdapter) UsersFragment.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
getJSON();
return rootview;
}
private void showEmployee(String json){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String email = jo.getString("Email");
String login = jo.getString("Login");
HashMap<String,String> employees = new HashMap<>();
employees.put("name",name);
employees.put("email",email);
employees.put("login",login);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row,
new String[]{"name","email"},
new int[]{R.id.nom, R.id.email2});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewMedecins.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("login").toString();
intent.putExtra("login", tid);
startActivity(intent);
}
fragment_users.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Editext for Search -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/inputSearch" />
<ListView android:id="@+id/listView"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
<ImageButton
android:id="@+id/btn1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_launcher"
android:layout_gravity="right|top" />
</FrameLayout>
list_row.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">
<TextView
android:id="@+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
UPDATE2
public class UserListActivity extends AppCompatActivity {
private String JSON_STRING;
public static final String URL="http://medicalrepresentative.comxa.com/Scripts/getAllProd.php";
ArrayList<Contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
// Lookup the recyclerview in activity layout
RecyclerView rvContacts = (RecyclerView) findViewById(R.id.rvContacts);
// Initialize contacts
contacts = Contact.createContactsList(20);
// Create adapter passing in the sample user data
ContactsAdapter adapter = new ContactsAdapter(contacts);
// Attach the adapter to the recyclerview to populate items
rvContacts.setAdapter(adapter);
// Set layout manager to position the items
rvContacts.setLayoutManager(new LinearLayoutManager(this));
// That's all!
}
}
Contact.java
public class Contact {
private String mName;
private String mRef;
public Contact(String name, String ref) {
mName = name;
mRef = ref;
}
public String getName() {
return mName;
}
public String getRef() {
return mRef;
}
public static ArrayList<Contact> createContactsList(int numContacts) {
ArrayList<Contact> contacts = new ArrayList<Contact>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
HashMap<String,String> employees = new HashMap<>();
employees.put("name",name);
employees.put("ref",ref);
contacts.add(new Contact(employees));
}
} catch (JSONException e) {
e.printStackTrace();
}
return contacts;
}
}
ContactsAdapter.java
public class ContactsAdapter extends
RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
public static final String URL =&#34; http://aaaa.com/getall/getusers.php&#34 ;;
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public TextView refTextView;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.nom);
refTextView = (TextView) itemView.findViewById(R.id.email2);
}
}
private List<Contact> mContacts;
// Pass in the contact array into the constructor
public ContactsAdapter(List<Contact> contacts) {
mContacts = contacts;
}
@Override
public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_contact, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Contact contact = mContacts.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
TextView textView2 = viewHolder.refTextView;
textView.setText(contact.getName());
textView2.setText(contact.getRef());
}
// Return the total count of items
@Override
public int getItemCount() {
return mContacts.size();
}
}
错误 错误:(33,46)错误:找不到符号变量json 错误:(43,46)错误:构造函数联系人类联系人无法应用于给定类型; required:String,String 发现:HashMap 原因:实际和正式的参数列表长度不同 错误:任务&#39;:app:compileDebugJavaWithJavac&#39;执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
答案 0 :(得分:0)
好的onBindViewHolder
你需要3个步骤:
我建议您创建一个空项目来了解RecyclerView
的工作原理。如果您了解它足以让您创建大量的RecyclerView
。祝你有个美好的一天。
答案 1 :(得分:0)
您需要在RecyclerView适配器中的onBideViewHolder()中设置数据。
在您的xml中使用RecyclerView标记而不是ListView标记。