我正在从SQLite
数据库中检索信息,并将其显示在RecyclerView
到ArrayList
中。我的代码工作正常,没有错误,但是,我试图显示的4 TextViews
中的1个无法显示,即使它遵循与其他3完全相同的方法,我不知道为什么。< / p>
使用RecyclerView的活动:
public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id, Name, Surname, Department, Workplace FROM LysandrosTable WHERE Name || ' ' || Surname LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
lysandrosHelper = new LysandrosDatabaseAdapter(this);
mRecyclerView.setAdapter(new EmployeeListAdapter(lysandrosHelper.getAllDat(), R.layout.custom_row2));
}
适配器类:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtFirstName_Recycler;
public TextView txtLastName_Recycler;
public TextView txtDepartment_Recycler;
public TextView txtWorkplace_Recycler;
public ViewHolder(View itemView) {
super(itemView);
txtFirstName_Recycler = (TextView) itemView.findViewById(R.id.first_name_recycler);
txtLastName_Recycler = (TextView) itemView.findViewById(R.id.last_name_recycler);
txtDepartment_Recycler = (TextView) itemView.findViewById(R.id.department_recycler);
txtWorkplace_Recycler = (TextView) itemView.findViewById(R.id.workplace_recycler);
}
}
public void onBindViewHolder(ViewHolder holder, int position) {
DataBean item = items.get(position);
holder.txtFirstName_Recycler.setText(item.getName());
holder.txtLastName_Recycler.setText(item.getSurname());
holder.txtDepartment_Recycler.setText(item.getDepartment());
holder.txtWorkplace_Recycler.setText(item.getWorkplace());
}
我的数据库类使用相应的方法:
public List <DataBean> getAllDat(){
List<DataBean> list = new ArrayList<>();
SQLiteDatabase db = helper.getReadableDatabase();
String [] columns = {LysandrosHelper.UID, LysandrosHelper.NAME, LysandrosHelper.SURNAME, LysandrosHelper.DEPARTMENT, LysandrosHelper.WORKPLACE};
Cursor cursor = db.query(LysandrosHelper.TABLE_NAME, columns, null, null, null, null, null);
while (cursor.moveToNext()) {
int index = cursor.getColumnIndex(LysandrosHelper.UID);
int index2 = cursor.getColumnIndex(LysandrosHelper.NAME);
int index3 = cursor.getColumnIndex(LysandrosHelper.SURNAME);
int index4 = cursor.getColumnIndex(LysandrosHelper.DEPARTMENT);
int index5 = cursor.getColumnIndex(LysandrosHelper.WORKPLACE);
int cid = cursor.getInt(index);
String persoName = cursor.getString(index2);
String personSurname = cursor.getString(index3);
String personDepartment = cursor.getString(index4);
String personWorkplace = cursor.getString(index5);
DataBean bean = new DataBean(cid, persoName, personSurname, personDepartment, personWorkplace);
list.add(bean);
}
return list;
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:src="@drawable/ic_account_circle_black_48dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/first_name_recycler"
android:layout_toRightOf="@+id/icon"
android:layout_alignParentTop="true"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/last_name_recycler"
android:layout_toRightOf="@+id/first_name_recycler"
android:layout_toEndOf="@+id/first_name_recycler"
android:paddingLeft="7dp"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/department_recycler"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_below="@+id/first_name_recycler"
android:layout_toRightOf="@+id/icon"
android:layout_toEndOf="@+id/icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/workplace_recycler"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/department_recycler"
android:layout_toEndOf="@+id/department_recycler"
android:layout_below="@+id/last_name_recycler"
android:paddingLeft="7dp"
android:textSize="20sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
TextView
未显示的是Workplace。
答案 0 :(得分:0)
我几乎可以肯定你的布局编写得很糟糕,first_name_recycler
内的本地化文本扩展会导致其他TextView的高度或宽度为0.基本上,如果你的一个textViews太大,那么& #39;没有其他的空间。
尝试更简单的布局,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:src="@drawable/ic_home"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/first_name_recycler"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/last_name_recycler"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/department_recycler"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/workplace_recycler"
android:textSize="20sp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
它应该有用。