2017-02-14 74 views
0

我正在傳遞ListView中傳遞值的對象的主要活動中的回調。如果我吐司吐司顯示的鍵,值對。我想把它添加到TopListCursorAdapter來填充一個新行。我在topAdapter.notifyDataSetChanged()上得到空值。將對象傳遞給CursorAdapter變爲null

不知道如何mEmployee添加到適配器,我試圖

@Override 
public void onBottomListClick(Employee e) { 
    mEmployee.add(e); 
    dbHandler.addEmployee(e); 
    SQLiteDatabase db = dbHandler.getWritableDatabase(); 
    final Cursor clickedEmployee = db.rawQuery("SELECT * FROM " + "employees" + " WHERE " + 
      "Employee_number" + "=" + e.getEmployee_number(), null); 
    // change the adapter's Cursor 
    topAdapter.changeCursor(clickedEmployee); 
} 

但我並不想通過光標和TopListCursorAdapter想要一個。我只想將mEmployee添加到TopListCursorAdapter中的現有列表中。

public class MainActivity extends FragmentActivity implements BottomListViewAdapter.BottomListClickListener { 
    private ProgressBar mProgressBar; 
    EmployeeDBHandler dbHandler; 
    private TopListCursorAdapter topAdapter; 
    private BottomListViewAdapter bottomAdapter; 
    private ArrayList mEmployee; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 
     dbHandler = new EmployeeDBHandler(getApplicationContext()); 
     mProgressBar.setVisibility(View.VISIBLE); 
     getXMLData(); 

     //GUI for seeing android SQLite Database in Chrome Dev Tools 
     Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this); 
     inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this)); 
     Stetho.Initializer in = inBuilder.build(); 
     Stetho.initialize(in); 
    } 

    public void getXMLData() { 
     OkHttpClient client = getUnsafeOkHttpClient(); 
     Request request = new Request.Builder() 
       .url(getString(R.string.API_FULL_URL)) 
       .build(); 
     client.newCall(request).enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       e.printStackTrace(); 
      } 

      @Override 
      public void onResponse(Call call, final Response response) throws IOException { 
       final String responseData = response.body().string(); 
       final InputStream stream = new ByteArrayInputStream(responseData.getBytes()); 
       final XMLPullParserHandler parserHandler = new XMLPullParserHandler(); 
       final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream); 

       for (Employee e : employees) { 
        dbHandler.addEmployee(e); 
       } 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         mProgressBar.setVisibility(View.GONE); 
         displayTopList(); 
         displayBottomList(); 
        } 
       }); 
      } 
     }); 
    } 

    public void displayTopList() { 
     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.topFragment, new TopFragment()); 
     fragmentTransaction.commit(); 
    } 

    public void displayBottomList() { 
     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.bottomFragment, new BottomFragment()); 
     fragmentTransaction.commit(); 
    } 

@Override 
    public void onBottomListClick(Employee e) { 
     mEmployee.add(e); 
     dbHandler.addEmployee(e); 
     SQLiteDatabase db = dbHandler.getWritableDatabase(); 
     final Cursor clickedEmployee = db.rawQuery("SELECT * FROM " + "employees" + " WHERE " + 
       "Employee_number" + "=" + e.getEmployee_number(), null); 
     // change the adapter's Cursor 
     topAdapter.changeCursor(clickedEmployee); 
    } 

}

TopListCursorAdapter

public class TopListCursorAdapter extends CursorAdapter { 
    private EmployeeDBHandler dbHandler; 
    private Activity activityRef; 

    public TopListCursorAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
     activityRef = (Activity) context; 
    } 

    @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) { 
     dbHandler = new EmployeeDBHandler(context); 
     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); 
     } 

     activityRef.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

      } 
     }); 


     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

我不想通過光標和TopListCursorAdapter想要一個

肯定。你有一個DBHandler,它可以給你一個Cursor

dbHandler = new EmployeeDBHandler(getApplicationContext()); 

而你有一個addEmployee方法。

dbHandler.addEmployee(e); 

所以,問題是 - 你是怎麼創建TopListCursorAdapter沒有,因爲它需要已經有一個Cursor


無論如何,你不應該在適配器中粘貼EmployeeDBHandler
它只需要一個Cursor。另外,你似乎從來沒有在那裏使用那門課。

public class TopListCursorAdapter extends CursorAdapter { 
    // private EmployeeDBHandler dbHandler; // ** Not needed 
    private Context mContext; // Don't need an Activity 

    public TopListCursorAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
     mContext = context; // Just a Context, no Activity 
    } 

你應該永遠不需要最初的創造一個後創建new TopListAdapter,你可以直接在現有的適配器上調用changeCursor

@Override 
public void onBottomListClick(Employee e) { 
    mEmployee.add(e); 
    dbHandler.addEmployee(e); 

    // topAdapter = new TopListCursorAdapter(); // ** Nope 
    Cursor newCursor = dbHandler.getEmployees(); // TODO: Implement this 
    topAdapter.changeCursor(newCursor); // Updates the UI itself 

    Intent employeeDetail = new Intent(MainActivity.this, EmployeeFullInfo.class); 
    employeeDetail.putExtra("Employee_number", e.getNumber()); 
    ... 
    startActivity(employeeDetail); 
} 

注意:如果您use ParcelableEmployee對象,你不需要一大堆的意圖putExtragetExtra方法。


另外,您可以將Employee對象存儲爲ViewHolder的一部分,以實現更簡單的數據管理。這樣,您只需要將Cursor中的數據提取到Employee中,那麼ViewHolder可以保留,因爲您正在複製onClick內的工作以獲取Cursor數據。

public static class ViewHolder { 
    Employee employee; // See here 
    TextView tvFirstName; 
    TextView tvLastName; 
    TextView tvTitle; 
    ImageView mPeepPic; 
    ImageButton mDetailsButton; 
    CardView mCardView; 
} 
+0

我有一個遊標從具有rawQuery獲取行添加到TopListView的Fragment傳遞到適配器。這是將員工添加到現有數據庫。該員工已經存在於數據庫中。 –

+0

我只是想指出'新的TopListCursorAdapter();'不是你想要的。你應該改爲調用'changeCursor',然而你得到那個Cursor需要從數據庫中查詢。 –

+0

試圖弄清楚如何獲得僱員,因爲我有getEmployee來接受employeeNumber並擁有一個接受ArrayList的getAllEmployees。我可以通過員工e的getEmployee employeeNumber將行添加到頂部列表中? –