如果 SQLite 为空或有记录,则更改片段中的视图
Posted
技术标签:
【中文标题】如果 SQLite 为空或有记录,则更改片段中的视图【英文标题】:Change View in Fragment if SQLite it is null or has a record 【发布时间】:2019-05-26 15:00:02 【问题描述】:我正在尝试更改我称之为历史的片段中的视图。 如果数据库 SQLite 有记录,我想显示列表视图,但如果有空显示另一个视图。 如果数据库已记录视图调用 history_item 作为布局和 historyName 显示 TextView 我想要如果数据库没有记录调用 fragment_history 那里有带有 ImageView 和 TextView 的 LinearLayout。
history_item.XML
<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_
android:layout_>
<ImageView android:id="@id/ivHistory" style="@style/ivHistory" />
<TextView android:id="@+id/historyName" android:layout_ 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_ android:layout_ android:text="@string/Delete" />
</LinearLayout>
FragmentHistory.class
public class FragmentHistory extends Fragment
View paramView;
public TextView textView, showDeleteButton, noSearch;
LinearLayout test;
public ImageView showHistoryQuery;
DatabaseHelper mDatabaseHelper;
private ListView mListView;
private LinearLayout noQueries;
public FragmentHistory()
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
paramView = inflater.inflate(R.layout.history, container, false);
textView = paramView.findViewById(R.id.historyName);
noSearch = paramView.findViewById(R.id.tvHistoryPage);
mListView = paramView.findViewById(R.id.listView);
noQueries = paramView.findViewById(R.id.linLayoutEmptyList);
mDatabaseHelper = new DatabaseHelper(getActivity());
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 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));
ListAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.history_item, R.id.historyName, listData);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
String name = parent.getItemAtPosition(position).toString();
Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
int itemID = -1;
while(data.moveToNext())
itemID = data.getInt(0);
if(itemID > -1)
Intent editScreenIntent = new Intent(getActivity(), ActivitySearchEngine.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
);
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
String name = parent.getItemAtPosition(position).toString();
Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
int itemID = -1;
while(data.moveToNext())
itemID = data.getInt(0);
if(itemID > -1)
showDeleteButton = view.findViewById(R.id.btnDeleteHistory);
showHistoryQuery = view.findViewById(R.id.imgViewSetQuery);
showDeleteButton.setVisibility(View.VISIBLE);
showHistoryQuery.setVisibility(View.GONE);
final int finalItemID = itemID;
final String finalName = name;
showDeleteButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mDatabaseHelper.deleteName(finalItemID, finalName);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(FragmentHistory.this).attach(FragmentHistory.this).commit();
);
return true;
);
fragment_history.XML
<LinearLayout android:orientation="vertical" android:layout_
android:layout_
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/lvHistory" android:longClickable="true"
android:layout_ android:layout_
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_ android:layout_>
<ImageView android:layout_gravity="center" android:id="@+id/ivHistoryPage"
android:layout_
android:layout_
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_ android:layout_
android:text="@string/SVEmptyHistoryTableMessage" android:lineSpacingExtra="2.0sp" />
</LinearLayout>
</LinearLayout>
数据库类
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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);
【问题讨论】:
【参考方案1】:我能够解决这个问题,只是我在这些行中添加了这段代码,这就像魅力一样。
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);
【讨论】:
以上是关于如果 SQLite 为空或有记录,则更改片段中的视图的主要内容,如果未能解决你的问题,请参考以下文章