2017-02-13 101 views
-1

我有2個ListViews。一個在另一個。當用戶點擊bottomList中的一行時,我正在打一個Web服務並創建一個新的遊標並重新填充bottomList。同時,我想把點擊的行放到TopListView中。我試圖將一個Intent從cursorAdapter傳遞給MainActivity,但這只是用與那裏相同的值重新填充bottomListView。甚至不打電話給Web服務。 TopListView沒有任何反應。將CursorAdapter傳遞給另一個CursorAdapter

是否可以將onClick從bottomCursorAdapter的位置傳遞給topCursorAdapter?或者像employee_number這樣的一些變量,我可以在TopAdapter和swapCursor中查詢它?如果是這樣,不知道如何交換光標,因爲沒有辦法知道是否有人點擊了topListView中的bottomListView。

我TopList方法

public void displayTopList() { 
     SQLiteDatabase db = dbHandler.getWritableDatabase(); 
     mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null); 
     ListView mTopListView = (ListView) findViewById(R.id.mTopList); 
     TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, mTopCursor); 
     mTopListView.setAdapter(topAdapter); 
    } 

方法我創建從適配器獲得的數據顯示敬酒

public void onRowClick(String mEmployeeNumber, String mFirstName) { 
     Toast.makeText(getApplicationContext(), "SEND TO TOP LIST " + mEmployeeNumber + " " + mFirstName, Toast.LENGTH_LONG).show(); 
    } 

我TopAdapter,我嘗試添加onRowClick。

public class TopListCursorAdapter extends CursorAdapter { 
    public TopListCursorAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false); 
    } 

    @Override 
    public void bindView(View view, final Context context, final Cursor cursor) { 

     ViewHolder holder; 
     holder = new ViewHolder(); 
     holder.tvFirstName = (TextView) view.findViewById(R.id.personFirstName); 
     holder.tvLastName = (TextView) view.findViewById(R.id.personLastName); 
     holder.tvTitle = (TextView) view.findViewById(R.id.personTitle); 
     holder.mPeepPic = (ImageView) view.findViewById(R.id.person_photo); 
     holder.mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton); 
     holder.mCardView = (CardView) view.findViewById(R.id.home_screen_cardView); 

     String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name")); 
     String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name")); 
     String mPayrollTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title")); 
     String mThumbnail = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData")); 

     holder.tvFirstName.setText(mFirstName); 
     holder.tvLastName.setText(mLastName); 
     holder.tvTitle.setText(mPayrollTitle); 

     if (mThumbnail != null) { 
      byte[] imageAsBytes = Base64.decode(mThumbnail.getBytes(), Base64.DEFAULT); 
      Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); 
      holder.mPeepPic.setImageBitmap(parsedImage); 
     } else { 
      holder.mPeepPic.setImageResource(R.drawable.img_place_holder_adapter); 
     } 


     final int position = cursor.getPosition(); 
     holder.mDetailsButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       cursor.moveToPosition(position); 
       String mEmployeeNumber = cursor.getString(1); 
       String mEmail = cursor.getString(8); 
       String mFirstName = cursor.getString(2); 
       String mLastName = cursor.getString(3); 
       String mPhoneMobile = cursor.getString(4); 
       String mPhoneOffice = cursor.getString(5); 
       String mCostCenter = cursor.getString(10); 
       String mHasDirectReports = cursor.getString(7); 
       String mTitle = cursor.getString(6); 
       String mPic = cursor.getString(9); 
       Intent mIntent = new Intent(context, EmployeeFullInfo.class); 
       mIntent.putExtra("Employee_number", mEmployeeNumber); 
       mIntent.putExtra("Email", mEmail); 
       mIntent.putExtra("First_name", mFirstName); 
       mIntent.putExtra("Last_name", mLastName); 
       mIntent.putExtra("Phone_mobile", mPhoneMobile); 
       mIntent.putExtra("Phone_office", mPhoneOffice); 
       mIntent.putExtra("Cost_center_id", mCostCenter); 
       mIntent.putExtra("Has_direct_reports", mHasDirectReports); 
       mIntent.putExtra("Payroll_title", mTitle); 
       mIntent.putExtra("ThumbnailData", mPic); 
       mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       v.getContext().startActivity(mIntent); 
      } 
     }); 
    } 

    public static class ViewHolder { 
     TextView tvFirstName; 
     TextView tvLastName; 
     TextView tvTitle; 
     ImageView mPeepPic; 
     ImageButton mDetailsButton; 
     CardView mCardView; 
    } 
} 
+0

你熟悉的「回調」的概念? –

+0

如果不是,你基本上正在努力做到這一點。主細節流程。 https://developer.android.com/training/basics/fragments/communicating.html –

+0

這是否必須與碎片?使用我的onClick事件的類是CursorAdapters。我無法覆蓋CursorAdapter中的onAttach方法。 –

回答

2

如果我理解正確的話,你想擁有的代碼知道當一個click事件在一個適配器發生,然後發送一些數據到另一個。

在這種情況下,您可以簡單地定義一個接口,通過包含兩者的Activity類來橋接適配器之間的「通信」。

例如,這裏是您的接口定義的接口。當你點擊按鈕時它會被激活。

public class TopListCursorAdapter extends CursorAdapter { 

    public interface TopListClickListener { 
     void onTopListClick(int position); // Can add more parameters here 
    } 

    private TopListClickListener callback; 

    public TopListCursorAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
     if (!(context instanceof TopListClickListener)) { 
      throw new ClassCastException("Context must implement TopListClickListener"); 
     } 
     this.callback = (TopListClickListener) context; 
    } 

    ... 

     holder.mDetailsButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       cursor.moveToPosition(position); 
       ... 
       if (callback != null) { 
        callback.onTopListClick(position); // Can pass more parameters here 
       } 

而在活動中,您可以實現該接口(又名回調)

public class MainActivity extends AppCompatActivity 
    implements TopListCursorAdapter.TopListClickListener // see here 

    // private ... topAdapter, bottomAdapter; 

    @Override 
    public void onTopListClick(int position) { 
     // Make sure parameters match the interface ^^^ 


     // TODO: Get data from topAdapter 
     // TODO: database update or arraylist.add to change bottomAdapter 
     bottomAdapter.notifyDataSetChanged(); // Update the UI 
    } 

    // Other code can remain the same 
+0

我是否會將數據發送到另一個適配器,就像它是片段一樣?只需在MainActivity的方法中調用Adapter而不是Fragment?嘗試傳遞該點擊上的所有行信息並將其發送到其他適配器以將其添加到列表中。 –

+0

其他片段需要附加到活動,但我不明白爲什麼這不起作用。 –

+0

關於「通過所有行信息」。你可以創建一個'Employee'對象。你也可以'實施Parcelable'。但是,你不應該通過這個方法傳遞每個單獨的領域。 –