我有一个扩展SQLiteOpenHelper
的db数据列表。
我可以读取数据,可以保存新数据,但是问题是我想在ListView
下面显示另一个名为clear_history
的布局,所以我想做的就是向我展示{{1 }}和列表下方的ListView
。
我试图创建一个TextView
,但无法实现。
因为我想要一个textView,可用于单击侦听器并删除数据库列表。
以下是我想要的列表中的照片。
第一张照片是实际设计。
第二张照片是我想要的。
这是我的代码。
DatabaseHelper.class
RecyclerView Adapter
FragmentHistory.XML
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
db.execSQL(query);
}
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
db.execSQL(query);
}
}
history_item.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/lvHistory" android:longClickable="true"
android:layout_width="match_parent" android:layout_height="match_parent"
android:listSelector="@android:color/transparent" android:soundEffectsEnabled="false"
android:overScrollMode="never" />
<LinearLayout android:gravity="center" android:orientation="vertical"
android:id="@+id/linLayoutEmptyList" android:paddingBottom="32.0dip" android:visibility="gone"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:layout_gravity="center" android:id="@+id/ivHistoryPage"
android:layout_width="@dimen/history_query_empty"
android:layout_height="@dimen/history_query_empty"
android:layout_marginBottom="@dimen/history_query_empty_margin"
android:src="@drawable/ic_history" />
<TextView android:textSize="20.0sp" android:textColor="@color/blue_title"
android:gravity="center" android:layout_gravity="center" android:id="@+id/tvHistoryPage"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/SVEmptyHistoryTableMessage" android:lineSpacingExtra="2.0sp" />
</LinearLayout>
</LinearLayout>
FragmentHistory.class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/selector_btn_home"
android:id="@+id/historyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@id/ivHistory" style="@style/ivHistory" />
<TextView android:id="@+id/historyName" android:layout_height="38dp" style="@style/TextViewQuery"/>
<ImageView android:id="@+id/imgViewSetQuery" style="@style/ImageViewSetQuery" />
<TextView android:textSize="@dimen/search_font" android:textColor="@android:color/white"
android:gravity="center" android:id="@+id/btnDeleteHistory" android:background="@color/red" android:paddingLeft="5.0dip"
android:paddingRight="5.0dip" android:visibility="gone" android:longClickable="false"
android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/Delete" />
</LinearLayout>
clear_history.XML
public class FragmentHistory extends Fragment {
View paramView;
public TextView textView, showDeleteButton, noSearch;
public ImageView showHistoryQuery;
DatabaseHelper mDatabaseHelper;
private ListView mListView;
private LinearLayout noQueries;
ListAdapter listAdapter;
private final Handler handler = new Handler();
public FragmentHistory() {
}
private boolean switchOnOff;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.fragment_history, container, false);
textView = paramView.findViewById(R.id.historyName);
noSearch = paramView.findViewById(R.id.tvHistoryPage);
mListView = paramView.findViewById(R.id.lvHistory);
noQueries = paramView.findViewById(R.id.linLayoutEmptyList);
noSearch.setTextColor(Color.GRAY);
mDatabaseHelper = new DatabaseHelper(getActivity());
doTheAutoRefresh();
populateListView();
((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener() {
@Override
public void onRefresh() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(FragmentHistory.this).attach(FragmentHistory.this).commit();
}
});
return paramView;
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateView(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 1000);
}
private void populateListView() {
//get the data and append to a list
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
listData.add(data.getString(1));
}
if (data.getCount() != 0) {
ListAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.history_item, R.id.historyName, listData);
mListView.setAdapter(adapter);
} else {
mListView.setVisibility(View.GONE);
noQueries.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:1)
据我了解,您看不到“清晰的” TextView。如果是这种情况,只需转到您的XML,然后将ListView和TextView嵌套在垂直的线性布局中即可。然后调整您希望两者的显示方式。
答案 1 :(得分:1)
尝试一下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lvHistory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/linLayoutEmptyList"
android:listSelector="@android:color/transparent"
android:longClickable="true"
android:overScrollMode="never"
android:soundEffectsEnabled="false" />
<LinearLayout
android:id="@+id/linLayoutEmptyList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="32.0dip"
android:visibility="visible">
<ImageView
android:id="@+id/ivHistoryPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_fav" />
<TextView
android:id="@+id/tvHistoryPage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="2.0sp"
android:text="SVEmptyHistoryTableMessage"
android:textSize="20.0sp" />
</LinearLayout>
</RelativeLayout>