大家好 我正在尝试为我的公司构建一个应用程序。我已经将数据存储到SQLite中。在搜索界面中,我可以从数据库搜索并在简单的TextView上找到结果。但是我想通过使用RecyclerView查看实际结果。我已经在这里附加了我的代码以及适配器类。 问题是我无法在RecyclerView中查看查询数据。请帮我做到这一点。
- MainActivity.java
public class Client extends AppCompatActivity {
private static final String TAG = Client.class.getSimpleName();
ClientDatabase db;
ClientModel clientModel;
RequestQueue requestQueue;
Context context;
Button search_button;
EditText search_word;
TextView search_result;
RecyclerView recycler_view;
FinalAdapter adapter;
ArrayList<ClientModel> clients = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
db = new ClientDatabase(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
search_word = (EditText)findViewById(R.id.search_word);
search_result = (TextView)findViewById(R.id.search_result);
search_button = (Button)findViewById(R.id.search_button);
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showResult(v);
}
});
RequestData();
}
private void RequestData() {
Log.d(TAG, "RequestData: Requesting Data from API");
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String url = getResources().getString(R.string.url);
JSONObject jsonObject = new JSONObject();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray info = response.getJSONArray("response");
ClientDatabase clientDatabase = new ClientDatabase(getApplicationContext());
Log.d(TAG, "onResponse: Inserting data into Database");
for (int i =0; i<info.length(); i++){
JSONObject jsonObject = info.getJSONObject(i);
ClientModel clientModel = new ClientModel();
clientModel.setClient_name(jsonObject.getString("client_name"));
clientModel.setAddress(jsonObject.getString("address"));
clientModel.setNote(jsonObject.getString("note"));
clientModel.setCp_name(jsonObject.getString("cp_name"));
clientModel.setCp_phone(jsonObject.getString("cp_phone"));
clientModel.setCp_email(jsonObject.getString("cp_email"));
clientModel.setAssign_person_name(jsonObject.getString("assign_person_name"));
clientDatabase.AddClient(clientModel);
clients.add(clientModel);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error in saving",Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
}
private void _All() {
Log.d(TAG, "_All: Clients in ready");
Cursor cursor = db.ViewData();
}
private void showResult(View view){
try {
String word = search_word.getText().toString();
search_result.setText("Result for : " + word + ":\n\n");
//search for word in the database
Cursor cursor = db.search(word);
cursor.moveToFirst();
if (cursor != null && cursor.getCount() > 0){
int index1, index2, index3, index4;
String result;
do {
// Don't guess at the column index. Get the index for the named column.
index1 = cursor.getColumnIndex(db.COLUMN_CLIENT_NAME);
// Get the value from the column for the current cursor.
result = cursor.getString(index1);
// Add result to what's already in the text view.
search_result.append(result + "\n");
} while (cursor.moveToNext());
cursor.close();
} else {
search_result.append(getString(R.string.no_result));
}
} catch (Exception e){
e.printStackTrace();
}
}
private void initRecyclear(){
recycler_view = (RecyclerView)findViewById(R.id.recycler_view);
adapter = new FinalAdapter(context, clients);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
recycler_view.setAdapter(adapter);
}
@Override
protected void onDestroy() {
db.RemoveData();
super.onDestroy();
}
}
- FinalAdapter.java
public class FinalAdapter extends RecyclerView.Adapter<FinalAdapter.ViewHolder> {
Context context;
ArrayList<ClientModel> clientModelList;
public FinalAdapter(Context context, ArrayList<ClientModel> clientModelList) {
this.context = context;
this.clientModelList = clientModelList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.client_inflater, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.name.setText(clientModelList.get(position).getClient_name());
holder.phone.setText(clientModelList.get(position).getCp_phone());
holder.email.setText(clientModelList.get(position).getCp_email());
holder.assigned.setText(clientModelList.get(position).getAssign_person_name());
}
@Override
public int getItemCount() {
return 0;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView name, phone, email, assigned;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.client_name);
phone = (TextView) itemView.findViewById(R.id.client_phone);
email = (TextView) itemView.findViewById(R.id.client_email);
assigned = (TextView) itemView.findViewById(R.id.assign_name);
}
}
}
- 屏幕截图
答案 0 :(得分:1)
您错过了两分:
initRecyclear()
clients
ArrayList 解决方案
从initRecyclear()
方法调用onCreate()
:
initRecylear();
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showResult(v);
}
});
然后在RequestData()
方法内填充客户列表
private void RequestData() {
ClientModel cmObj = new ClientModel();
//Here Fetch your Data and put it to ClientModel object cmObj then add this object into your clients ArrayList
clients.add(cmObj);
//After Completion on data preparation call below method to notify your recycler
adapter.notifyDataSetChanged();
}
从RequestData()
事件中最终调用onClick
方法
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RequestData();
}
});