2017-09-01 34 views
0

關於如何在同一活動中顯示光標結果在兩個不同的列表視圖的任何想法?一個例子是從Whatsapp他們有(我推測)一個「聊天」表和「消息」表。一旦我們在工具欄上鍵入關鍵字,我將在1個活動中查詢兩個表並在兩個不同的列表視圖中顯示。有沒有辦法做到這一點?或者它們只在1個單一列表視圖中顯示?請指教。在一個活動中的Android顯示光標結果在兩個不同的列表視圖

謝謝。

Whatsapp

回答

1

是可以做到這一點。爲了以簡單的方式模擬上述內容,假定數據庫有兩個表聊天和消息。它們都有兩列,分別是文本的唯一標識符和列。

使用SQLiteOpenHelper的子類,我們可以有一個文件名爲DBHlpr.java爲: -

public class DBHlpr extends SQLiteOpenHelper { 

    static final String DBNAME = "mydb"; 
    static final String CHATTABLE = "chats"; 
    static final String MSGTABLE = "messages"; 

    static final String TEXTCOL = "textdata"; 
    static final String IDCOL = "_id"; 


    DBHlpr(Context context) { 
     super(context,DBNAME,null,1); 
    } 

    public void onCreate(SQLiteDatabase db){ 
     db.execSQL("CREATE TABLE " + CHATTABLE + " (" + IDCOL + " INTEGER PRIMARY KEY, " + TEXTCOL + " TEXT)"); 
     db.execSQL("CREATE TABLE " + MSGTABLE + " (" + IDCOL + " INTEGER PRIMARY KEY, " + TEXTCOL + " TEXT)"); 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { 
    } 

    public void insertRow(String table, String text) { 
     ContentValues cv = new ContentValues(); 
     cv.put(TEXTCOL,text); 
     long id = this.getWritableDatabase().insert(table,null,cv); 
     Log.d("DBHLP-INSRT","Added row with ID=" + Long.toString(id)); 
    } 

    public Cursor getRows(String table, String srchstr) { 

     SQLiteDatabase db = getWritableDatabase(); 

     String whereclause = null; 
     if (srchstr.length() > 0) { 
      whereclause = TEXTCOL + " LIKE '%" + srchstr + "%' "; 
     } 
     return db.query(
       table, 
       null,whereclause,null,null,null,null); 
    } 
} 

onCreate方法,即創建2個表聊天消息(按CHATTABLE和MSGTABLE)。

onUpgrade is required but does nothing as yet.

insertRow可以用於將行添加到任何一個表(因爲它們都具有相同的結構)。請注意,它會寫入日誌,以便插入可以很容易地被確認。

getRows用於獲取包含數據的光標(來自數據庫的數據)。這一個方法也可以用於這兩個表。它傳遞一個字符串(如果它的長度大於0)來過濾表(按照在搜索字段中輸入的數據)。

活動將需要一個佈局指定兩個列表視圖,在這種情況下,這將是activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="mjt.so45787986.MainActivity"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Search" /> 
    <EditText 
     android:id="@+id/search" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Chats" /> 
    <ListView 
     android:id="@+id/chats" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </ListView> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Messages" /> 
    <ListView 
     android:id="@+id/messages" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </ListView> 
</LinearLayout> 

沒有什麼特別的,這是一個非常簡單的佈局,有一個EditText的搜索將TextView標準化爲聊天列表(以下ListView)的標題,然後針對消息使用另一個TextView和ListView。

MainActivity.java可能是: -

public class MainActivity extends AppCompatActivity { 

    DBHlpr dbhlpr = new DBHlpr(this); 
    Cursor chatcursor; 
    Cursor msgcursor; 
    TextWatcher tx; 
    EditText search; 
    ListView chatlist; 
    ListView msglist; 
    SimpleCursorAdapter chat_sca; 
    SimpleCursorAdapter msg_sca; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     search = (EditText) findViewById(R.id.search); 
     chatlist = (ListView) findViewById(R.id.chats); 
     msglist = (ListView) findViewById(R.id.messages); 


     // Add some data for testing 

     dbhlpr.insertRow(DBHlpr.CHATTABLE, "This is my first chat"); 
     dbhlpr.insertRow(DBHlpr.CHATTABLE, "This is my second chat"); 
     dbhlpr.insertRow(DBHlpr.CHATTABLE, "This is my third chat"); 
     dbhlpr.insertRow(DBHlpr.MSGTABLE, "First Message"); 
     dbhlpr.insertRow(DBHlpr.MSGTABLE, "Second Message"); 
     dbhlpr.insertRow(DBHlpr.MSGTABLE, "Third Message"); 

     chatcursor = dbhlpr.getRows(DBHlpr.CHATTABLE,""); 
     chat_sca = new SimpleCursorAdapter(
       this, 
       android.R.layout.simple_list_item_1, 
       chatcursor, 
       new String[]{DBHlpr.TEXTCOL}, 
       new int[]{android.R.id.text1}, 
       0 
       ); 
     chatlist.setAdapter(chat_sca); 
     msgcursor = dbhlpr.getRows(DBHlpr.MSGTABLE,""); 
     msg_sca = new SimpleCursorAdapter(
       this, 
       android.R.layout.simple_list_item_1, 
       msgcursor, 
       new String[]{DBHlpr.TEXTCOL}, 
       new int[]{android.R.id.text1}, 
       0 
     ); 
     msglist.setAdapter(msg_sca); 

     search.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       refreshCursors(); 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 

      } 
     }); 
    } 

    private void refreshCursors() { 
     chatcursor = dbhlpr.getRows(DBHlpr.CHATTABLE,search.getText().toString()); 
     chat_sca.swapCursor(chatcursor); 
     msgcursor = dbhlpr.getRows(DBHlpr.MSGTABLE,search.getText().toString()); 
     msg_sca.swapCursor(msgcursor); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     chatcursor.close(); 
     msgcursor.close(); 
    } 
} 

首先是一些類變量的定義: -

DBHlpr dbhlpr = new DBHlpr(this); 
    Cursor chatcursor; 
    Cursor msgcursor; 

是數據庫相關的。第一個創建DBHlpr的實例,另外兩個用於2個遊標(聊天和消息)。

TextWatcher tx; 
EditText search; 
ListView chatlist; 
ListView msglist; 

用於處理視圖。 TextWatcher用於檢測對搜索的更改。

SimpleCursorAdapter chat_sca; 
    SimpleCursorAdapter msg_sca; 

用於使數據庫數據適應ListViews。顧名思義,這些都是非常基礎的,但會用於演示目的。

onCreate的前5行非常標準。

接下來的6行基本上都是相同的,即dbhlpr.insertRow(DBHlpr.CHATTABLE, "This is my first chat");,並調用insertRow方法傳遞相應的表和要存儲的數據。應該注意的是,第一次運行的第一次是創建數據庫的時間。注意這些行僅用於提供一些測試數據。

然後我們來(注意代碼以下這基本上是重複,但是對信息表) -

chatcursor = dbhlpr.getRows(DBHlpr.CHATTABLE,""); 
    chat_sca = new SimpleCursorAdapter(
      this, 
      android.R.layout.simple_list_item_1, 
      chatcursor, 
      new String[]{DBHlpr.TEXTCOL}, 
      new int[]{android.R.id.text1}, 
      0 
      ); 
    chatlist.setAdapter(chat_sca); 

第一行獲得與各自的數據光標(所有聊天行,爲按照第二個參數)。第二到第九行是設置Simple Cursors Adapater的單個命令(第一個參數是上下文,第二個是使用的佈局(我們作弊和使用簡單的一個),第三個是保存數據的Cursor,第四個是要獲取遊標中的數據的列,第5個是將要放置數據的視圖ID,第6個應該是0)。 最後一行告訴ListView使用適配器。

在此之後,將TextWatcher添加到搜索中。在此使用onTextChanged方法,該方法調用refreshCursors方法。

refreshCursors方法使用已更改的搜索數據將數據從數據庫中獲取到現有遊標中,然後通過swapCursor告知adapter數據已更改。

最後的onDestroy方法用於在Activity即將結束時關閉遊標。

在最初運行: -

enter image description here

在搜索欄中鍵入˚F後: -

enter image description here

+0

謝謝。 @MikeT。 –

相關問題