如何在ListView下显示另一个布局 [英] How to show another layout below the ListView

查看:67
本文介绍了如何在ListView下显示另一个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展SQLiteOpenHelper的db数据列表. 我可以读取数据,可以保存新数据,但是问题是我想在ListView下面显示另一个名为clear_history的布局,所以我想做的就是向我显示ListView列表下方. 我试图创建RecyclerView Adapter,但无法实现. 因为我想要一个textView,可用于单击侦听器并删除数据库列表. 以下是我想要的列表中的照片. 第一张照片是实际设计. 第二张照片是我想要的.

I have a db data list which extends SQLiteOpenHelper. I can read the data, I can save new data but the problem it is I want below the ListView to show another layout which is named clear_history so what I am trying to do is show me ListView and the TextView below the list. I was trying to create a RecyclerView Adapter but I wasn't able to achieve that. Because I want to have a textView which I can use for click listener and do delete the db list. Below are the photos from the list I want. First photo is the actual design. Second photo is what I am trying to have.

这是我的代码.

DatabaseHelper.class

DatabaseHelper.class

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);
    }
}

FragmentHistory.XML

FragmentHistory.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>

history_item.XML

history_item.XML

<?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>

FragmentHistory.class

FragmentHistory.class

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);
        } 

}

clear_history.XML

clear_history.XML

<TextView
    android:id="@+id/btnClearSearch"
    android:textSize="15sp"
    android:textColor="@android:color/black"
    android:background="@drawable/selector_btn_home"
    android:paddingLeft="@dimen/history_left_padding"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@string/SVSRClearHistoryCellTitle"
    android:layout_height="fill_parent">

</TextView>

推荐答案

据我了解,您看不到清晰的" TextView.如果是这种情况,只需转到您的XML,然后将ListView和TextView嵌套在垂直的线性布局中即可.然后调整您希望两者的显示方式.

From what I understand, you cannot see your "clear" TextView. If that is the case, just go to your XML and nest the ListView and TextView in a vertical linear layout. Then adjust how you want the two to appear.

这篇关于如何在ListView下显示另一个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆